XMLStarlet 根据值返回属性(反向查找)

问题描述:

我有这个 xml:

<block name="http-www.example.com">
  <mapentry tag="AB12">bottlecap</mapentry>
  <mapentry tag="CD45">description goes here</mapentry>
  <mapentry tag="GH23">12345</mapentry>
  <mapentry tag="JK89">54321</mapentry>
</block>

我想根据值找到标签".

And I want to find the "tag" based on the value.

所以我想搜索bottlecap"并让它返回AB12"

So I want to search for "bottlecap" and have it return "AB12"

在我见过的所有示例中,人们搜索属性以获得结果,但在这种类型的 xml 中这是不可能的,因为节点和属性不明确

In all the examples I've seen, people search for the attribute to get the result, but in this type of xml that isn't possible since the nodes and attributes are ambiguous

我一直在尝试使用 -i 但这似乎只适用于节点或属性,而不适用于值.

I've been trying to use -i but that only seems to work on the node or attribute, but not on the value.

xmlstarlet sel -t -i //bottlecap -v "//mapentry[@tag]" "mytest.xml"

有没有办法搜索值?

也许这就是你想要的?

xmlstarlet sel -t -v "//mapentry[. = 'bottlecap']/@tag" "mytest.xml"