在 XQuery 中使用 functx:index-of-match-first 返回文本节点的子字符串
我正在尝试编写一个 XQuery,它将查找 xml 文件中包含给定关键字的所有文本节点。文本节点很长,所以我想返回从匹配关键字开始的文本子字符串(所需长度)。
Samplefile.xml
<books>
<book>
<title>linear systems</title>
<content>vector spaces and linear system analysis </content>
</book>
<book>
<title>some title</title>
<content>some content</content>
</book>
</books>
Samplexquery.xq
declare namespace functx = "http://www.functx.com";
for $match_result in /*/book/*[contains(.,'linear')]/text()
return substring($match_result, functx:index-of-match-first($match_result,'linear'), 50)
我期望得到结果[线性系统,线性系统分析]。第一本书的标题节点包含单词“线性”。返回从“线性...”开始的 50 个字符。第一本书的内容节点也是如此。
我使用的是 XQuery 1.0,并且包含了名称空间 functx,如以下示例所示: http://www.xqueryfunctions.com/xq/functx_index-of-match-first.html
但是,这给了我一个错误:[XPST0017]未知函数“functx:index-of-match-first (...)”。
谢谢, 索尼
I am trying to write an XQuery which would find all text nodes that contain a given key word in an xml file. The text node is long so I would like to return a substring(of a desired length) of the text starting at the matching key word.
Samplefile.xml
<books>
<book>
<title>linear systems</title>
<content>vector spaces and linear system analysis </content>
</book>
<book>
<title>some title</title>
<content>some content</content>
</book>
</books>
samplexquery.xq
declare namespace functx = "http://www.functx.com";
for $match_result in /*/book/*[contains(.,'linear')]/text()
return substring($match_result, functx:index-of-match-first($match_result,'linear'), 50)
I expect to get the result [linear systems, linear system analysis]. The title node of first book contains the word 'linear'. Return 50 characters starting from 'linear....'. Similarly for the content node of first book.
I am using XQuery 1.0 and I included the namespace functx as shown in the example at: http://www.xqueryfunctions.com/xq/functx_index-of-match-first.html
But, this is giving me an error: [XPST0017] Unknown function "functx:index-of-match-first(...)".
Thanks,
Sony
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅声明名称空间是不够的。
您还必须拥有该函数的代码。仅预定义标准 XQuery 和 XPath 函数和运算符在语言中。
此更正的代码:
应用于提供的 XML 文档时(已更正多个格式不正确的错误):
产生预期结果:
< strong>使用
导入模块
从现有函数库导入模块的指令。
It is not sufficient to only declare the namespace.
You must also have the code of the function. Only the standard XQuery and XPath functions and operators are predefined in the language.
This corrected code:
when applied on the provided XML document (with several non-well-formedness errors corrected):
produces the expected result:
It is a good practice to use the
import module
directive to import modules from existing libraries of functions.