XMLStarlet 根据值返回属性(反向查找) [英] XMLStarlet Return attribute based on value (Reverse lookup)

查看:15
本文介绍了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"

这篇关于XMLStarlet 根据值返回属性(反向查找)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆