我有一个需要从 shell 解析的 XML 文件。它看起来有点像这样:
<?xml version="1.0" encoding="UTF-8"?>
<things>
<thing>
<foo>123</foo>
<bar>false</bar>
</thing>
<thing>
<bar>true</bar>
<foo>abc</foo>
</thing>
</things>
请注意,thing
s 的子节点的顺序是任意的。在我的真实数据中,每个thing
.
现在, 的价值foo
是独一无二的。我需要获取子节点的值,例如其子bar
节点thing
的foo
值abc
。我尝试过使用 XPath 查询,xmllint
但没有成功。我找不到比赛;或者,如果我只是用更一般的查询进行测试,我会得到整个树。
$ xmllint --pattern //thing[foo='abc']/bar test.xml
xmllint: No match.
$ xmllint --pattern //thing/bar test.xml
(the whole tree)
我希望只是得到true
。
我究竟做错了什么?我无法访问--xpath
提到的其他问题的标志,所以如果这是答案,那么我就不走运了。