xmlstarlet选择值

joe*_*epd 16 xml xmlstarlet

这是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>
Run Code Online (Sandbox Code Playgroud)

我试图找到有价值property2的项目.我可以选择数据:identifyer1234

$ xmlstarlet sel -t -c "/DATA/TABLES/ITEM/identifyer [@V=1234]" test.xml 
<identifyer V="1234"/>
Run Code Online (Sandbox Code Playgroud)

需要两种类型的输出:

$ xmlstarlet <some magic>
<identifyer V="1234"></identifyer>
<property1 V="abcde"></property1>
<Property2 V="qwerty"></property2>
Run Code Online (Sandbox Code Playgroud)

和:

$ xmlstarlet <some magic>
qwerty
Run Code Online (Sandbox Code Playgroud)

npo*_*avs 33

关键是从ITEM节点开始,而不是从标识符开始:

$ xmlstarlet sel -t -c "/DATA/TABLES/ITEM[identifyer/@V=1234]" test.xml
<ITEM>
  <identifyer V="1234"/>
  <property1 V="abcde"/>
  <Property2 V="qwerty"/>
</ITEM>
Run Code Online (Sandbox Code Playgroud)

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

$ 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
Run Code Online (Sandbox Code Playgroud)