选择 1) 在给定节点之前但 2) 也是另一个给定节点的后代的节点 [英] Select nodes that 1) precede a given node but 2) are also descendants of another given node
本文介绍了选择 1) 在给定节点之前但 2) 也是另一个给定节点的后代的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下 XML:
<div id="global-header">标题
<div class="a"><h3>一些标题</h3><p>文本1</p><p>文本2</p><p>文本3</p>
我想
- 找到任何值为text 2"的
节点,然后
- 查找在此特定
<p>
之前但也是<div class='a'>
节点的后代的所有节点.
所需的输出应如下所示:
一些标题
<p>文本1</p>
需要注意的是,前面的节点可能包含任意节点类型,而不仅仅是 和
,如上例所示.>
我的第一次尝试:
.//p[text()=text 2"]/preceding::*
不幸的是,这也会选择
,这是不希望的.
解决方案
您需要使用 preceding-sibling
来选择属于同一父级的子节点,而不是 preceding
>:
.//p[text()=text 2"]/preceding-sibling::*
Say I have the following XML:
<body>
<div id="global-header">
header
</div>
<div class="a">
<h3>some title</h3>
<p>text 1</p>
<p>text 2</p>
<p>text 3</p>
</div>
</body>
I want to
- find any
<p>
node whose value is "text 2", and then - find all the nodes that precede this particular
<p>
but are also descendants of the<div class='a'>
node.
The desired output should look like:
<h3>some title</h3>
<p>text 1</p>
The caveat is that the preceding nodes may contain arbitrary node type, not only <h3>
and <p>
, as in the above case.
My first try:
.//p[text()="text 2"]/preceding::*
Unfortunately, this will also select <div id="global-header">
, which is not desired.
解决方案
You need to use preceding-sibling
to select nodes that are children of the same parent instead of preceding
:
.//p[text()="text 2"]/preceding-sibling::*
这篇关于选择 1) 在给定节点之前但 2) 也是另一个给定节点的后代的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文