如何使用单个 xpath 表达式获得 H1,H2,H3,... [英] How to get H1,H2,H3,... using a single xpath expression
本文介绍了如何使用单个 xpath 表达式获得 H1,H2,H3,...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在单个 xpath 表达式中获取 H1、H2、H3 内容?
How can I get H1,H2,H3 contents in one single xpath expression?
我知道我可以做到这一点.
I know I could do this.
//html/body/h1/text()
//html/body/h2/text()
//html/body/h3/text()
等等.
推荐答案
使用:
/html/body/*[self::h1 or self::h2 or self::h3]/text()
以下表述不正确:
//html/body/*[local-name() = "h1"
or local-name() = "h2"
or local-name() = "h3"]/text()
因为它可能选择作为unwanted:h1
、different:h2
、someWeirdNamespace:h3
的子节点的文本节点.
because it may select text nodes that are children of unwanted:h1
, different:h2
, someWeirdNamespace:h3
.
另一个建议:当 XML 文档的结构是静态已知的时,始终避免使用 //
.使用 //
通常会导致显着的低效率,因为它会导致遍历在上下文节点中旋转的完整文档(子)树.
Another recommendation: Always avoid using //
when the structure of the XML document is statically known. Using //
most often results in significant inefficiencies because it causes the complete document (sub)tree roted in the context node to be traversed.
这篇关于如何使用单个 xpath 表达式获得 H1,H2,H3,...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文