XQuery 参考手册

XQuery 终极参考手册(2025 版)

—— 真正写代码时直接复制粘贴用的速查表(全网最全、最实用的单文件版)

类别函数 / 语法写法(直接复制)说明 / 经典场景
版本与声明XQuery 版本声明xquery version "3.1";必须写在最前面
编码xquery version "3.1" encoding "utf-8";
命名空间声明declare default element namespace "http://example.com/books";
加载文档单文件doc("books.xml")
doc("file:///c:/data/books.xml")
目录所有 XMLcollection("db/books?select=*.xml")BaseX/eXist
MarkLogiccts:search(fn:collection(), cts:true())
路径表达式所有 book//book
有 category 属性//book[@category]
category = “WEB”//book[@category = "WEB"]
//book[@category = ("WEB","XML")]
多值推荐写法
第 n 个//book[5]
最后 1 个//book[last()]
倒数 3 个//book[position() > last()-3]
位置 3 到 10//book[position() = 3 to 10]
FLWOR 经典模板排序 + 分页for $b in //book order by xs:decimal($b/price) descending return $b
分页(第 $page 页,每页 $size)let $start := ($page - 1) * $size + 1 return subsequence($sorted, $start, $size)
按类别分组统计(3.1)for $c in distinct-values(//book/@category) group by $c return <cat name="{$c}" count="{count($books)}" avg="{avg($books/price)}"/>
字符串函数包含(不区分大小写)contains(lower-case($title), lower-case($kw))最常用搜索
正则匹配matches($title, "XQuery|XPath", "i")i = ignore case
拆分tokenize($tags, "\s+")
拼接string-join($author, ", ")
替换replace($phone, "\D", "")只留数字
去首尾空格+多余换行normalize-space($desc)必写!
数值函数强制转数字xs:decimal($book/price)number($book/price)
四舍五入round($price * 0.9, 2)
平均/最大/最小avg($books/price)max()min()
日期时间当前时间current-dateTime()current-date()
格式化format-date(current-date(), "[Y]-[M01]-[D01]")
北京时间adjust-dateTime-to-timezone(current-dateTime(), xs:dayTimeDuration("PT8H"))
序列操作去重distinct-values(//book/@category)神器
取前 n 个(//book)[position() <= 10]subsequence(//book, 1, 10)
删除第 n 个remove($seq, 3)
倒序reverse(//book)
构造新 XML复制旧节点 + 加新属性/元素<book isbn="{$old/@isbn}" category="NEW">{ $old/*, <stock>100</stock> }</book>最常用模式
计算构造函数(动态标签名)element { $tagName } { attribute id { $id }, $content }
输出 JSON(3.1)数组array { //book ! map { "title": string(title) } }
对象map { "total": count(//book), "items": [上行数组] }
XQuery Update添加属性insert node attribute category {"SALE"} into $b
添加子元素(最后)insert node <inStock>true</inStock> as last into $b
在某个节点后插入insert node <discount>20%</discount> after $b/price
替换属性值replace value of node $b/@category with "TECH"
删除节点delete node $b/price
自定义函数简单函数declare function local:discount($p as xs:decimal) as xs:decimal { if ($p>100) then $p*0.8 else $p*0.9 };
带默认参数(3.1)declare function local:format($p, $cur := "USD") { concat($cur, $p) };
调试打印变量trace($var, "当前价格: ")BaseX/Saxon
性能技巧避免在 where 里做类型转换where $b/price > 100(price 已经是 decimal)而不是 xs:decimal($b/price)
使用 let 缓存重复路径let $books := //book[@category="WEB"] return count($books)

30 秒速记口诀(贴在显示器旁边)

//找 → [条件] → for/let/where/order → <新标签>{}</新标签> → JSON 用 map/array
搜索 → lower-case + contains/matches
分页 → subsequence($seq, $start, $size)
分组 → distinct-values() + group by
改文档 → insert node … into / as last / after

把这张表保存为 xquery-cheat-sheet.txt 或打印出来,写 XQuery 再也不用到处找文档了!

需要我给你:

  • Markdown 版(可直接放 Notion/Obsidian)
  • PDF 打印版
  • BaseX / eXist-db / MarkLogic 专属扩展函数表

随时说一声,我立刻打包发你!

文章已创建 2783

发表回复

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

相关文章

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

返回顶部