Why doesn't xmlstarlet select all nodes?

痴心易碎 提交于 2019-11-28 08:55:43

问题


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



来源:https://stackoverflow.com/questions/6390807/why-doesnt-xmlstarlet-select-all-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!