xmlstarlet选择值

前端之家收集整理的这篇文章主要介绍了xmlstarlet选择值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是xml数据:
  1. <DATA VERSION="1.0">
  2. <TABLES>
  3. <ITEM>
  4. <identifyer V="1234"></identifyer>
  5. <property1 V="abcde"></property1>
  6. <Property2 V="qwerty"></property2>
  7. </ITEM>
  8. <ITEM>
  9. <identifyer V="5678"></identifyer>
  10. <Property1 V="zyxwv"></property1>
  11. <Property2 V="dvorak"></property2>
  12. </ITEM>
  13. </TABLES>
  14. </DATA>

我正在尝试找到物品的属性2,其中识别者的值为1234.我可以选择数据:

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

两种类型的输出将是可取的:

  1. $xmlstarlet <some magic>
  2. <identifyer V="1234"></identifyer>
  3. <property1 V="abcde"></property1>
  4. <Property2 V="qwerty"></property2>

和:

  1. $xmlstarlet <some magic>
  2. qwerty
关键是从ITEM节点开始,而不是识别器:
  1. $xmlstarlet sel -t -c "/DATA/TABLES/ITEM[identifyer/@V=1234]" test.xml
  2. <ITEM>
  3. <identifyer V="1234"/>
  4. <property1 V="abcde"/>
  5. <Property2 V="qwerty"/>
  6. </ITEM>

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

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

猜你在找的XML相关文章