XPATH selecting the root element

78,432

Solution 1

I'm guessing you are in a predicate here? and want to return to look at data higher up the tree for your condition?

You should be able to start with a leading / and then work your way back down e.g

/vehicles/cars/car[@id = /vehicles/featuredVehicle/@id]

Solution 2

An XPath expression starting with / is always referring to the root element. Look here for the syntax and some useful sample queries

If you want to select the root element itself, simply use /<element name> or /*

Solution 3

You can use the ancestor axis. Say you have this document:

<a><b><c></c></b></a>

Then a/b/c/ancestor::a brings the context to the c node and then back to the a node.

Solution 4

Yes. Use "/" as your XPath expression. This will select the root element for you. "//" selects the root and all its descendants.

Share:
78,432
heymega
Author by

heymega

Updated on April 05, 2020

Comments

  • heymega
    heymega over 4 years

    Is there a simple way of moving back to the root node within any given context. The XML document I'm working with is extremely large and would require using ../.. about a dozen times!!

    Any help is greatly appreciated guys.

  • heymega
    heymega over 12 years
    Ahh I was under the impression I was stuck in a context!! thanks :)