user1424739
user1424739

Reputation: 13683

Is there a way to specify the lowest ancestor in XPath?

ancestor means Selects all ancestors (parent, grandparent, etc.) of the current node according to https://www.w3schools.com/xml/xpath_axes.asp.

Is there a way to specify the lowest ancestor instead of all the ancestors?

Upvotes: 2

Views: 320

Answers (1)

kjhughes
kjhughes

Reputation: 111630

The lowest ancestor would be the parent, which is given by .. or parent::node().

The highest ancestor would be the root node, which is given by /; or, if you want the highest ancestor element: /*.

See also What is the difference between root node, root element and document element in XML?

Note that if you'd like to select the lowest ancestor that satisfies a predicate, append a [1] to the predicate – ancestors are ordered upward from the starting point, not downward from the root. For example,

//e[@id="e1"]/ancestor::*[@class][1]

would select the lowest ancestor of the e1 e element that has a @class attribute.

Upvotes: 3

Related Questions