这是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>
我正在尝试找到物品的属性2,其中识别者的值为1234.我可以选择数据:
- $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节点开始,而不是识别器:
- $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
- qwerty