XPath函数通过字符串处理、节点筛选和逻辑判断等功能,显著提升路径表达式的灵活性与精准度。典型函数如contains()和starts-with()用于模糊匹配属性值,应对动态class或href;normalize-space()清理文本中的冗余空白,提升数据质量;count()和position()支持列表项的定量分析与索引定位;结合text()、substring-before()、translate()等可精准提取复杂文本中的目标信息,如价格、库存数字;在多语言场景中,通过关键词判断语言类型;面对不规则结构时,利用following-sibling、name()、local-name()实现相对定位与泛化选择,增强选择器的鲁棒性。

XPath函数,说白了,就是你在构建路径表达式时,用来给筛选节点、处理数据加点“魔法”的工具。它能让你在原本只能按层级、按属性找节点的基础上,进行字符串操作、数值计算、布尔逻辑判断,甚至对节点集进行更复杂的处理。在我看来,掌握这些函数,是把XPath从“能用”提升到“好用”的关键一步,尤其是在面对那些结构不那么规整,或者你需要从文本里抠出特定信息的时候。
使用XPath函数,你通常会把它们嵌入到你的路径表达式中,无论是作为谓词(
[]
函数名(参数)
我们可以大致把XPath函数分为几类,每类都有其独特的应用场景:
节点集函数 (Node Set Functions): 这类函数主要用于处理或获取节点集的信息。
count(node-set)
count(//li)
<li>
position()
//div[position() = 2]
<div>
last()
//p[position() = last()]
<p>
id(string)
id('main-content')main-content
字符串函数 (String Functions): 用于处理节点或属性的文本内容。
string(object)
string(//h1)
<h1>
concat(string1, string2, ...)
concat('Hello', ' ', 'World')contains(string1, string2)
string1
string2
//a[contains(@href, 'product')]
href
starts-with(string1, string2)
string1
string2
//div[starts-with(@class, 'item-')]
class
<div>
substring(string, start, length)
substring('HelloWorld', 6, 5)string-length(string)
//p[string-length(text()) > 50]
<p>
normalize-space(string)
normalize-space(//div[@class='description']/text())
布尔函数 (Boolean Functions): 返回
true
false
not(boolean)
//a[not(@target='_blank')]
target
true()
false()
数值函数 (Number Functions): 用于执行数学计算。
number(object)
sum(node-set)
floor(number)
ceiling(number)
round(number)
这些函数可以单独使用,也可以嵌套使用,形成非常强大的表达式。比如,你想找一个
class
div
//div[contains(@class, 'card') and contains(text(), '优惠')]
and
在我日常的网页抓取和XML处理工作中,有那么几个XPath函数是我的“常客”,用得频率非常高,它们各自解决的问题也挺典型的。
首先是
contains()
starts-with()
class
id
item-12345
product-card-abc
item-
product-card-
starts-with(@class, 'item-')
href
category=electronics
contains(@href, 'category=electronics')
接着是
normalize-space()
normalize-space(text())
count()
position()
count(//ul/li)
//ul/li[position() < 3]
position()
最后,不得不提
text()
//div[@class='price']/text()
string(.)
normalize-space(.)
在实际的数据抓取中,复杂的文本和属性值是常态,而不是例外。XPath函数在这里能发挥出巨大的作用,让你的选择器变得更加智能和灵活。
举个例子,假设你要从一个商品详情页抓取价格。价格信息可能不是一个简单的数字,它可能是“¥ 129.99起”或者“原价: 200.00,现价: 150.00”。 如果你的目标是提取“129.99”,你可以先用
normalize-space()
substring-before()
substring-after()
translate()
substring-before(normalize-space(//div[@class='price']/text()), '起')
translate(substring-before(normalize-space(//div[@class='price']/text()), '起'), '¥', '')
再比如,处理属性值。很多网站的图片URL可能藏在
data-src
class
class="product-item large active"
active
//div[contains(@class, 'active')]
active
//div[contains(@class, 'product-item') and contains(@class, 'active')]
有时候,链接的
href
starts-with()
substring-before()
substring-before(@href, '?')
我还遇到过一种情况,需要从一个混杂了文字和数字的字符串中提取纯数字。比如“库存:123件”。这时候,
translate()
translate(., '库存:件', '')
number(translate(., '库存:件', ''))
处理多语言内容和不规则HTML结构,是XPath函数真正展现其“高级”之处的场景。这不仅仅是找到元素,更是要以一种灵活且健壮的方式去适应变化。
对于多语言内容,虽然XPath 1.0中没有直接的语言检测函数(XPath 2.0+有
lang()
//h2[contains(text(), '新闻') or contains(text(), '最新')]
//h2[contains(text(), 'News') or contains(text(), 'Latest')]
lang
matches()
translate()
string-length()
至于不规则HTML结构,这才是XPath函数大显身手的地方。网页开发者并不总是按照W3C标准来写HTML,或者为了快速迭代,结构会变得非常“自由”。
following-sibling::*
preceding-sibling::*
position()
//h2[text()='产品详情']/following-sibling::p[position()=1]
结合
或
:** 有时候,你不知道具体的标签名是什么,或者标签名可能会变(比如从
变成
//*[starts-with(name(), 'h')]
h1
h6
local-name()
//p[string-length(normalize-space(text())) > 0]
<p>
id()
id
id('some-id')总而言之,面对复杂和不规则的结构,XPath函数提供了一种“以不变应万变”的策略。它们允许你根据元素的行为、内容或相对位置来定位,而不是仅仅依赖于其静态的层级和标签名。这需要一些经验和对HTML结构的洞察,但一旦掌握,你的XPath技能会提升一个档次。
以上就是XPath函数如何使用?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号