Fetching multiple values using xmllint

This can be done in xmllint with following XPath expression. Let suppose your XML is named hosts.xml.

xmllint hosts.xml --xpath '//host[starts-with(@name,"blah")]/@name | //host[starts-with(@name,"blah")]/@serial' | xargs -n2

This is because the | operator in an XPath expression can support several paths.


Perhaps it would suffice to simply select all attributes of the host element?

$ xmllint example.xml --xpath "//hosts/host[starts-with(@name,"blah")]/@*" > out.txt
$ cat out.txt
  name="blah001" serial="ABC001" name="blah002" serial="ABC002"

If that's not enough - if the output should be structured in a certain way, I'd recommend you write a simple XSLT transformation or use XQuery.

EDIT

so I will follow your suggestion and use xslt or xquery

Good, in case you decide for XSLT, the stylesheet you need would look similar to:

XSLT Stylesheet (1.0)

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" encoding="UTF-8" />

    <xsl:template match="host[starts-with(@name,'blah')]">
        <xsl:value-of select="concat('HOST: ',@name, ' SERIAL: ', @serial)"/>
    </xsl:template>
</xsl:transform>

Output

 HOST: blah001 SERIAL: ABC001
 HOST: blah002 SERIAL: ABC002

Tags:

Xml

Xpath

Xmllint