有没有办法指定XPath中的最低祖先?

时间:2019-07-30 22:06:20

标签: xml xpath

ancestor根据https://www.w3schools.com/xml/xpath_axes.asp的意思是Selects all ancestors (parent, grandparent, etc.) of the current node

是否可以指定最低祖先而不是所有祖先?

1 个答案:

答案 0 :(得分:2)

最低祖先 是父代,由..parent::node()给出。

最高祖先 将是根节点,由/给出;或者,如果您想要最高的祖先元素:/*

另请参阅What is the difference between root node, root element and document element in XML?

请注意,如果要选择满足谓词的最低祖先,请在谓词后附加[1]-祖先从起点向上排序,而不是从根向下排序。例如,

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

将选择具有e1属性的e @class元素的最低祖先。