xmlstarlet 选择值 [英] xmlstarlet select value

查看:37
本文介绍了xmlstarlet 选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是xml数据:

<DATA VERSION="1.0">
  <TABLES>
    <ITEM>
      <identifyer V="1234"></identifyer>
      <property1 V="abcde"></property1>
      <Property2 V="qwerty"></property2>
    </ITEM>
    <ITEM>
      <identifyer V="5678"></identifyer>
      <Property1 V="zyxwv"></property1>
      <Property2 V="dvorak"></property2>
    </ITEM>
  </TABLES>
</DATA>

我正在尝试查找 identifyer 具有值 1234 的项目的 property2.我可以选择数据:

I am trying to find property2 of the item where identifyer has value 1234. I can select the data:

$ xmlstarlet sel -t -c "/DATA/TABLES/ITEM/identifyer [@V=1234]" test.xml 
<identifyer V="1234"/>

需要两种类型的输出:

$ xmlstarlet <some magic>
<identifyer V="1234"></identifyer>
<property1 V="abcde"></property1>
<Property2 V="qwerty"></property2>

还有:

$ xmlstarlet <some magic>
qwerty

推荐答案

关键是从ITEM节点开始,而不是identifier:

The key is to start from the ITEM node, not the identifyer:

$ xmlstarlet sel -t -c "/DATA/TABLES/ITEM[identifyer/@V=1234]" test.xml
<ITEM>
  <identifyer V="1234"/>
  <property1 V="abcde"/>
  <Property2 V="qwerty"/>
</ITEM>

然后你可以挑选你想要的位:

Then you can pick out the bits you want:

$ xmlstarlet sel -t -c "/DATA/TABLES/ITEM[identifyer/@V=1234]/*" test.xml
<identifyer V="1234"/><property1 V="abcde"/><Property2 V="qwerty"/>

$ xmlstarlet sel -t -v "/DATA/TABLES/ITEM[identifyer/@V=1234]/Property2/@V" test.xml
qwerty

这篇关于xmlstarlet 选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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