路径与序列
XQuery 内置大量字符串和日期函数,处理文本和时间的场景直接调用。
字符串函数
concat、string-length、substring、upper-case、lower-case、contains、starts-with、ends-with、replace、tokenize(拆分)、string-join(拼接序列)。
日期函数
current-date()、current-time()、year-from-date()、days-from-duration()。日期可以直接比较。
代码示例
(: 字符串 :)
substring("XML 入门教程", 1, 3) (: "XML" :)
upper-case("xml") (: "XML" :)
contains("XQuery", "Query") (: true :)
string-join(("A", "B", "C"), "-") (: "A-B-C" :)
tokenize("a,b,c", ",") (: ("a","b","c") :)
replace("2025-01-01", "-", "/") (: "2025/01/01" :)
(: 日期 :)
current-date()
year-from-date(current-date())
(: 查询 2024 年后的文章 :)
for $a in doc("news.xml")//article
where $a/pubDate > xs:date("2024-01-01")
return $a/title