xpath to select all parents and all children xpath to select all parents and all children xml xml

xpath to select all parents and all children


For the first query use:

//role[@key = 'add-user']/ancestor::*

For the second:

//role[@key = 'security-admin']//*


You always have a context node that you define as being an element with a specific attribute value, e.g. "add-user" or "security-admin":

//*[@key = "string"]

As this gives you a nodeset the expression for the context node needs to exclude all non-single elements of such a kind:

//*[@key = "string" and count(//*[@key = "string"]) = 1]

This solves the needs for your context node. You can then represent that contextnode with either the . or write it in there verbatim.

Select all parents, grandparents, grand-grandparents and so on of the context element (that is the ancestor axis):

./ancestor::*//*[@key = "string" and count(//*[@key = "string"]) = 1]/ancestor::*

Select all children (that is the descendant axis):

./descendant::*.//*//*[@key = "string" and count(//*[@key = "string"]) = 1]/descendant::*//*[@key = "string" and count(//*[@key = "string"]) = 1]//*