2012-09-28 19 views
12

Este es el XML de datos:xmlstarlet seleccione el valor de

<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> 

Estoy tratando de encontrar property2 del elemento en el que identifyer tiene valor 1234. Puedo seleccionar los datos:

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

Dos tipos de salida serían deseables:

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

Y:

$ xmlstarlet <some magic> 
qwerty 

Respuesta

19

La clave es empezar desde el nodo artículo, no el identifyer :

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

Luego puede elegir los bits que desee:

$ 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 
+0

Genial, eso funciona :) Es bueno ver la sintaxis en un ejemplo, puedo seguir adelante con esto. ¡Gracias! – joepd

Cuestiones relacionadas