David Bonnin
David Bonnin

Reputation: 302

xmlstarlet select value based on attribute

With the following file.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<config>
 <index type="I8">
  <book>2</book>
 </index>
</config>

I can't select book with

xmlstarlet sel --template --match /config/index[@type="I8"] -c . file.xml

I can't select book with

xmlstarlet sel --template --match /config/index[@type='I8'] -c . file.xml

I can select book with

xmlstarlet sel --template --match "/config/index[@type='I8']" -c . file.xml

I can select book with

xmlstarlet sel --template --match '/config/index[@type="I8"]' -c . file.xml

Also if type is type="8" in xml, I can select it with:

xmlstarlet sel --template --match /config/index[@type="8"] -c . file.xml

Why?

xmlstarlet 1.6.1
compiled against libxml2 2.9.4, linked with 20904
compiled against libxslt 1.1.29, linked with 10129

Upvotes: 1

Views: 940

Answers (1)

Luuk
Luuk

Reputation: 14900

This one works (tested on Windows and on Ubuntu):

xmlstarlet sel -t -i /config/index/@type=\"I8\" -m //book -c . -b file.xml

It has something to do with quoting, you can see why when looking at the output of next statement:

xmlstarlet sel -C --template --match /config/index[@type="I8"] -c . file.xml

it's output:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="no"/>
  <xsl:template match="/">
    <xsl:for-each select="/config/index[@type=I8]">
      <xsl:copy-of select="."/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

which is missing quotes around the I8.

Upvotes: 2

Related Questions