XPath: How to: select all children and grandchildren (regardless of depth) with the given attribute name of the current context?

eros picture eros · Jun 9, 2011 · Viewed 11.6k times · Source

xml:

<root0>
    <elem1>
        <elem21 xlink:href="21"/>
        <elem22>
            <elem31 xlink:href="31"/>
            <elem32>
                <elem41 xlink:href="41"/>
            </elem32>
        </elem22>
    </elem1>
</root0>

the depth is unknown. How can I select all elements with xlink:href attribute?

I tried the following:

*[@xlink:href]
self::*[@xlink:href]

Any guidance is appreciated.

Answer

cordsen picture cordsen · Jun 9, 2011

For just children of grandchildren use

descendant-or-self::*[@xlink:href]

For all nodes just add // in front of your xpath

//*[@xlink:href]

Also, your xml sample is not valid but I'm guessing it is just a sample.