检索具有特定属性名称的所有 xml 元素的节点名称
我有一个 xml,我们称之为 example.xml,如下所示:
<a>
<b ref="lala">text of node b
</b>
<c ref="hihi">text of node c
</c>
<d somethingelse="foo">text of the node d
</d>
</a>
我想获取所有具有名为“ref”属性的节点的名称,
因此我的输出将是:
b
c
我已经尝试过: xmlstarlet sel -t -v "name(//*[@ref])" example.xml
但我只得到第一个节点的名称作为输出,即 b
。获取所有节点名称的命令中缺少什么?
注意:我在 debian bulleyes 上运行,这是我的 xmlstarlet 版本:
xmlstarlet --version
1.6.1
compiled against libxml2 2.9.10, linked with 20910
compiled against libxslt 1.1.34, linked with 10134
I have an xml, let's call it example.xml that looks like this:
<a>
<b ref="lala">text of node b
</b>
<c ref="hihi">text of node c
</c>
<d somethingelse="foo">text of the node d
</d>
</a>
I want to get the names of all the nodes that have an attribute called "ref"
so my output would be:
b
c
I have tried:xmlstarlet sel -t -v "name(//*[@ref])" example.xml
but I only get the name of the first node as an output i.e., b
. What is missing in my command to get all the node names?
Note: I run on debian bulleyes and here is my xmlstarlet version:
xmlstarlet --version
1.6.1
compiled against libxml2 2.9.10, linked with 20910
compiled against libxslt 1.1.34, linked with 10134
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发现一些似乎有效的东西:
-n
是在不同行上给出输出的标签。可能是版本问题(不确定)。无论如何,这篇关于 xmlstarlet 的唯一输出与多个输出的文章给了我一些帮助:
为什么 xmlstarlet 不选择所有节点?
Found something that seems to work:
The
-n
is the tag to give the output on different lines.It might have been a problem of version (not sure). Anyway this post about unique vs multiple output with xmlstarlet gave me some help:
Why doesn't xmlstarlet select all nodes?
尝试使用此方法来获取具有
"ref"
属性的节点名称:Try this one to get names of nodes with
"ref"
attribute: