2

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?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
user1424739
  • 11,937
  • 17
  • 63
  • 152
  • 1
    Not sure I understand. The "lowest" ancestor is always root `/`. – PaulProgrammer Jul 30 '19 at 22:08
  • 1
    @PaulProgrammer, I believe that most would consider `/` to be the "highest" ancestor, and `parent::node()` to be the "lowest". (Trees in computer science are traditionally drawn with the root higher than the leaves. Also, document ordering reinforces the notion of the bottom of the document being "lower" than the top.) – kjhughes Jul 30 '19 at 23:49
  • Deepest, nearest etc... – PaulProgrammer Jul 30 '19 at 23:54

1 Answers1

3

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.

kjhughes
  • 106,133
  • 27
  • 181
  • 240