Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath select nodes with attribute names

Tags:

xml

xpath

Suppose We have a XML document given as

<MyDocument>
<Pages>
    <Page>
        <Para>
            <Word show="yes" wo="2">Some</Word>
            <Word>People</Word>
        </Para>
    </Page>
    <Page>
        <Para>
            <Word>Some</Word>
            <Word show="yes">Other</Word>
            <Word show="yes" wo="1">People</Word>
        </Para>
    </Page>
</Pages>
</MyDocument>

how can we find all the Word nodes with both attribute 'show' and 'wo'? I tried XPath //[@show] | //[@wo] but this expression selects node with @show as well. and //*[@show @wo] is not legal expression.

thanks

like image 991
Korben Avatar asked Apr 06 '26 18:04

Korben


1 Answers

how can we find all the Word nodes with both attribute 'show' and 'wo'?

Use and:

//Word[@show and @wo]

Or, you can also have multiple conditions in separate square brackets:

//Word[@show][@wo]
like image 187
alecxe Avatar answered Apr 08 '26 08:04

alecxe