Search code examples
xmlxpathxmllint

Fetching multiple values using xmllint


I'd like to use xpath from xmllint to fetch a hostname and a serial number.

Here is the XML

<hosts>
 <host name="blah001" serial="ABC001">
  <moreinfo />
 </host>
 <host name="blah002" serial="ABC002">
  <moreinfo />
 </host>
 ..
</hosts>

I can fetch all serials with host name blah*:

/ > cat //hosts/host[starts-with(@name,"blah")]/@serial
 -------
 serial="ABC001"
 -------
 serial="ABC002"

But I also want to see which hostname had that serial. Is that possible?


Solution

  • 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