这是xml-data:
<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。我可以选择数据:
$ 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发布于 2012-09-29 01:53:09
关键是从项目节点开始,而不是从标识符开始:
$ xmlstarlet sel -t -c "/DATA/TABLES/ITEM[identifyer/@V=1234]" test.xml
<ITEM>
<identifyer V="1234"/>
<property1 V="abcde"/>
<Property2 V="qwerty"/>
</ITEM>然后你可以挑选出你想要的部分:
$ 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
qwertyhttps://stackoverflow.com/questions/12640152
复制相似问题