xpathxpath-2.0

XPath for elements at multiple levels?


I have a following XML

<?xml version="1.0" encoding="UTF-8"?>
<stationary>
    <textbook>
        <name>test</name>
    </textbook>
    <notebook>
        <books>
            <name>test</name>
        </books>
    </notebook>
</stationary>

I am trying to get all name elements irrespective of their position in stationary

I have tried using the following syntax but it didn't work:

stationary/*/name/text()

Solution

  • Just use the following expression with a relative path:

    //name
    

    This seems to capture the two <name> tags you have in your XML sample:

    Element='<name>test</name>'
    Element='<name>test</name>'