考虑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
仅返回COOKING。如何获取所有名为category的属性?
额外问题:如何评估针对 XML 文件的 XPath 查询,而无需在编辑器或程序中打开它(我想要一个快速工具)。问题在于将大文件加载到内存中会导致编辑器崩溃。难道 eXist 是我最好的选择吗?
最佳答案
这是您需要的吗?
xml sel -t -m "//@category" -v "." -o " " books.xml
或者将每行的结果分开
xml sel -t -m "//@category" -v "." -n books.xml
更新
你是对的,你的 XPath 很好,你只是需要不同的参数。
关于xpath - 为什么 xmlstarlet 不选择所有节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6390807/