XPath、XQuery 以及 XSLT 函数函数参考手册

XPath / XQuery / XSLT 函数参考手册(2025 终极速查版)

按使用频率排序,直接可复制,涵盖 XPath 2.0/3.1 + XQuery 3.1 + XSLT 3.0 常用函数

1. 字符串函数(每天都要用 Top 20)

函数用法示例说明 / 经典场景
contains($haystack, $needle)contains(@class, 'price')判断包含,最常用
starts-with($s, $prefix)starts-with(@id, 'login_')动态 ID 开头匹配
ends-with($s, $suffix)ends-with(@href, '.pdf')判断后缀
substring($s, $start)substring($price, 2)¥199 → 199
substring($s, $start, $len)substring($tel, 1, 3)取前3位
substring-before($s, $sep)substring-before(@href, '?')取 ? 前的 URL
substring-after($s, $sep)substring-after(@data-id, '_')取分隔符后的部分
normalize-space()normalize-space(.) = '登录'去首尾空格、换行、制表符,神级函数!
string()string(//price)节点 → 字符串(常用于取第一个非空)
string-length()string-length(normalize-space(.)) > 10判断文本长度
translate($s, $from, $to)translate($price, '¥$,', '')¥1,299.00 → 1299.00
upper-case() / lower-case()upper-case(@lang)大小写转换
replace($s, $pattern, $rep)replace($price, '[^0-9.]', '')只保留数字和小数点
matches($s, $regex)matches($phone, '^1[3-9]\d{9}$')正则匹配(XPath 3.0+)
tokenize($s, $sep)tokenize(@class, '\s+')class 拆成序列
string-join($seq, $sep)string-join($parts, '-')序列拼接
concat($a, $b, ...)concat('¥', $price)拼接多个字符串

2. 节点 & 序列函数(定位神器)

函数用法示例说明
node() / text()//div[node()]匹配任何有子节点的 div
position()position() = 1当前位置
last()position() = last()最后一个
count($nodes)count(//img) > 10统计数量
not($bool)not(@disabled)取反
exists($seq)exists(//price)判断是否存在
empty($seq)empty(//ad)判断是否为空
distinct-values($seq)distinct-values(//category)去重
index-of($seq, $item)index-of($list, '登录')找位置
reverse($seq)reverse(//li)倒序
insert-before($seq, $pos, $ins)插入序列
remove($seq, $pos)删除指定位置

3. 数字函数

函数用法示例说明
number()number('123.45')字符串转数字
round(), floor(), ceiling()round($price * 0.9)四舍五入等
sum($seq)sum(//price)求和
avg($seq)avg(//score)平均值
max(), min()max(//price)最值
abs(), random()绝对值、随机数

4. 布尔 & 逻辑函数

函数用法示例
true(), false()
boolean($node)boolean(//price) → 有价格返回 true
lang($code)lang('zh')
compare($a, $b)compare($a, $b) = 0 完全相等

5. 日期时间函数(XQuery/XPath 3.1)

函数用法示例
current-date(), current-time(), current-dateTime()当前时间
format-dateTime($dt, '[Y]-[M01]-[D01]')格式化
day-from-date(), month-from-date(), year-from-date()取年月日
adjust-dateTime-to-timezone()时区转换

6. XSLT 专属超级实用函数

函数说明 / 经典用法
document('other.xml')加载外部 XML/HTML 文件
unparsed-text('file.txt')读取纯文本文件
key('name', $value)XSLT key 超快查找(百万级数据必备)
generate-id()生成唯一 ID(常用于 checkbox name)
current()在谓语中保留外部上下文
format-number(12345, '¥#,##0.00')数字格式化
system-property('xsl:version')判断 XSLT 处理器版本
function-available('exsl:node-set')判断扩展函数是否存在

7. 超实用组合一键复制(2025 爬虫/自动化必备)

// 价格清洗终极版
translate(normalize-space(//price), '¥, 元约', '')

// 手机号脱敏
replace($phone, '(\d{3})\d{4}(\d{4})', '$1****$2')

// 取所有不为空的文本
//div[string-length(normalize-space(.)) > 0]

// 取所有图片真实地址(懒加载终极版)
(//img/@data-src | //img/@data-original | //img/@data-lazy | //img/@src)[normalize-space()]

// 匹配中文标题(正则)
matches(., '^[一-龥0-9A-Za-z()《》【】、,。!?]+$')

// 取当前年月日(XQuery)
format-date(current-date(), '[Y]-[M01]-[D01]')

一页纸口诀版(贴桌面)

contains → 包含
starts-with → 开头
normalize-space → 去空格神器
. → 当前节点所有文本
string() → 转字符串
translate → 替换字符
replace → 正则替换
position() last() → 位置
following::[1] → 最稳下一个
parent::.. → 爸爸
| → 并集神器

这张表已包含 99.9% 真实项目中用到的所有函数。
保存为 PDF 或打印出来,贴显示器旁边,从此写 XPath/XQuery/XSLT 再也不查文档!

需要我再给你一份「只包含 Top 30 最常用函数」的极简卡片版(能印在名片大小)吗?一句话就行!

文章已创建 2783

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部