您输入的文档格式不正确。它缺少唯一的最外层(根)元素,并且某些属性值没有引号。给定一个像这样格式正确的输入文档:
<root>
<node>
<node text="text1" x="400" y="400"></node>
</node>
<node>
<node text="text1" x="400" y="900"></node>
</node>
<node>
<node text="text2" x="400" y="1000"></node>
</node>
</root>
The following XPath expression:
//node[@text = 'text1' and not(@y < preceding::node[@text = 'text1']/@y or @y < following::node[@text = 'text1']/@y)]
查找具有text1
和@y中最高值的node
元素。在这种情况下,唯一的结果是
<node text="text1" x="400" y="900"/>
更详细地说,路径表达式意味着:
//node select all `node` elements, anywhere in
the document
[@text = 'text1' but only if its `text` attribute is
equal to "text1"
and not(@y < preceding::node[@text = 'text1']/@y and only if the value of its `y`
attribute is not lower than the value
of an `y` attribute of another `node`
element of the same sort that precedes
the current one
or @y < following::node[@text = 'text1']/@y)] and only if the value if its `y`
attribute is not lower than the value
of an `y` attribute of another `node`
element of the same sort that follows
the current one
编辑:或者,使用
//node[@text="text1" and not(//node[@text="text1"]/@y > @y)]
正如splash58所建议的那样。这个表达式的语义与上面的表达式非常相似。
//node[@text="text1" and not(//node[@text="text1"]/@y > .//@y)]
- 这样更短。这不正确吗? - splash58@y
代替.//@y
。 - Mathias Müller