如何选择具有特定节点作为子节点的第一个前兄弟? [英] How to select the first preceding-sibling which has a particular node as child?
问题描述
我一直在尝试编写一个 XPath 语句,该语句将选择节点的第一个前同级.那个兄弟节点应该有一个特定的节点作为它的子节点.
I have been trying to write a XPath statement which will select the first preceding-sibling of a node. That sibling should have a particular node as its child.
例如:
<a>
<c></c>
....
<b></b>
</a>
<a></a>
....
....
<a></a>
<a>
<b> start </b>
</a>
在上述 XML 中,如果我的上下文位于节点 a
处,该节点在其子节点 b
中有 start
文本.如何选择子节点为 b
的前一个节点 a
?
In the above XML, if my context is at the node a
which has start
text inside its child b
. How can I select the preceding node a
whose child is b
?
我尝试了 preceding-sibling::*[b][1]
但它只选择节点 a
如果 b
是它的第一个节点孩子.我也试过 preceding-sibling::*[1][b]
但它只检查前面的第一个 a
节点,如果它不包含 b
,测试失败.
I tried preceding-sibling::*[b][1]
but it only selects the node a
if b
is its first child. I also tried preceding-sibling::*[1][b]
but it only checks the first preceding a
node and if it doesn't contains b
, the test fails.
注意:我使用的是 XSLT 1.0
Note: I am using XSLT 1.0
谁能给我建议一个解决方案?
Can anyone suggest me a solution?
提前谢谢!!
推荐答案
我相信:
preceding-sibling::*[b][1]
或者最好:
preceding-sibling::a[b][1]
应该可以正常工作.
我尝试过previous-sibling::*[b][1] 但它只选择节点a if b是它的第一个孩子.
I tried preceding-sibling::*[b][1] but it only selects the node a if b is its first child.
我不这么认为.
这篇关于如何选择具有特定节点作为子节点的第一个前兄弟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!