-->

Why doesn't xmlstarlet select all nodes?

2019-01-28 09:18发布

问题:

Consider this example from w3schools:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>

</bookstore>

xmlstarlet sel -t -v "//@category" books.xml

returns only COOKING. How to get all attributes named category?

BONUS question: how do I evaluate XPath queries against an XML file without opening it in an editor nor program (I want a quick tool). The issue is that loading the huge file into memory crashes the editor. Don't tell me that eXist is my best bet?

回答1:

Is this what you need?

xml sel -t -m "//@category" -v "." -o " " books.xml

or to separate the results on each line

xml sel -t -m "//@category" -v "." -n books.xml

Update

You're right, your XPath was good, you just need different parameters.



回答2:

Regarding why the original doesn't work: XSLT 1.0 value-of is defined to work like XPath's string() function, and string(node-set) => string(first-node). See also Feature Request: value-of multiple nodes should concat values of all nodes