user3459346
user3459346

Reputation: 45

how can extract information from xml file with xpath

i have this xml file

<A>
    <aa IRI="X"/>
    <bb IRI="X1"/>
    <bb IRI="X2"/>
</A>
<A>
    <aa IRI="Y"/>
    <bb IRI="Y1"/>
    <bb IRI="Y2"/>
</A>
<A>
    <aa IRI="Z"/>
    <bb IRI="Z1"/>
    <bb IRI="Z2"/>
</A>

. . .

in my xml file, it contains big number of A balise
so how can i extract the bb atribute (IRI) when aa IRI="Y"

Upvotes: 2

Views: 77

Answers (2)

StuartLC
StuartLC

Reputation: 107247

Alternatively, like so:

//A[aa/@IRI='Y']/bb/@IRI

(Find me the A which has an aa child element with IRI attribute of Y, then navigate to the bb subelement and retrieve the IRI attribute).

Upvotes: 1

alecxe
alecxe

Reputation: 473863

Check for the precending-sibling:

//A/bb[preceding-sibling::aa[@IRI="Y"]]/@IRI

Demo (using xmllint tool):

$ cat input.xml 
<test>
    <A>
        <aa IRI="X"/>
        <bb IRI="X1"/>
        <bb IRI="X2"/>
    </A>
    <A>
        <aa IRI="Y"/>
        <bb IRI="Y1"/>
        <bb IRI="Y2"/>
    </A>
    <A>
        <aa IRI="Z"/>
        <bb IRI="Z1"/>
        <bb IRI="Z2"/>
    </A>
</test>
$ xmllint input.xml --xpath '//A/bb[preceding-sibling::aa[@IRI="Y"]]/@IRI'
 IRI="Y1" IRI="Y2"

Upvotes: 1

Related Questions