开发者

Why doesn't xmlstarlet select all nodes?

开发者 https://www.devze.com 2023-03-14 04:44 出处:网络
Consider this example from w3schools: <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> <bookstore>

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</aut开发者_运维问答hor>
  <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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消