用于选择自身、前后节点的 XPath 表达式 [英] XPath expression to select self, preceding and following nodes
问题描述
我想根据 TARGET 的内容在文档中选择以下 HTML.IE.如果 TARGET 匹配,则选择所有内容.但是,我不知道该去哪里:id('page')/x:div/span/a='TARGET'
– 如何使用父、子和兄弟表达式来获取包含 div
、div
前面的 a
和 div<后面的两个
br
标签/代码>
<div><br/><跨度><a>目标</a><a></a><跨度><跨度><a></a></span><a></a><span></span></span><跨度><a></a></span></span>
<br/><br/>
使用单个 XPath,例如:
"///*[(self::a and following-sibling::*[1][self::div and span/a='TRAGET']) 或(self::div 和 span/a='TARGET') 或(self::br 和previous-sibling::*[1][self::div and span/a='TARGET']) 或(self::br 和previous-sibling::*[2][self::div 和span/a='TARGET'])]"
请注意,由于未关闭 br 标签,您的文档格式不正确.此外,我没有包含任何命名空间,您可以根据需要添加.
I'd like to select the following HTML in a document, based on the content of TARGET. I.e. if TARGET matches, select everything. However, I'm not sure where to go after: id('page')/x:div/span/a='TARGET'
– How to use parent, child, and sibling expressions to get the containing div
, the a
preceding that div
, and the two br
tags following the div
<a></a>
<div>
<br />
<span>
<a>TARGET</a>
<a></a>
<span>
<span>
<a></a>
</span>
<a></a>
<span></span>
</span>
<span>
<a></a>
</span>
</span>
</div>
<br />
<br />
Use a single XPath like:
"//*[
(self::a and following-sibling::*[1][self::div and span/a='TRAGET']) or
(self::div and span/a='TARGET') or
(self::br and preceding-sibling::*[1][self::div and span/a='TARGET']) or
(self::br and preceding-sibling::*[2][self::div and span/a='TARGET'])
]"
Do note that your document is not well formed due to unclosed br tags. Moreover, I didn't include any namespace, which you can add if necessary.
这篇关于用于选择自身、前后节点的 XPath 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!