在 XQuery 中使用 functx:index-of-match-first 返回文本节点的子字符串

发布于 2024-10-08 04:01:12 字数 1062 浏览 0 评论 0原文

我正在尝试编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不会写诗 2024-10-15 04:01:12

我正在使用 XQuery 1.0,并且我包含了
命名空间 functx 如所示
示例位于:
http://www.xqueryfunctions.com/xq/functx_index-of-match -first.html

但是,这给了我一个错误:
[XPST0017] 未知功能
“functx:匹配第一个索引(...)”。

仅声明名称空间是不够的。

您还必须拥有该函数的代码。仅预定义标准 XQuery 和 XPath 函数和运算符在语言中。

此更正的代码

declare namespace functx = "http://www.functx.com"; 
declare function functx:index-of-match-first 
  ( $arg as xs:string? ,
    $pattern as xs:string )  as xs:integer? {

  if (matches($arg,$pattern))
  then string-length(tokenize($arg, $pattern)[1]) + 1
  else ()
 } ;

 for $match_result in /*/book/*[contains(.,'linear')]/text()
  return substring($match_result, functx:index-of-match-first($match_result,'linear'), 50)

应用于提供的 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>

产生预期结果

linear systems linear system analysis

< strong>使用导入模块从现有函数库导入模块的指令。

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(...)".

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:

declare namespace functx = "http://www.functx.com"; 
declare function functx:index-of-match-first 
  ( $arg as xs:string? ,
    $pattern as xs:string )  as xs:integer? {

  if (matches($arg,$pattern))
  then string-length(tokenize($arg, $pattern)[1]) + 1
  else ()
 } ;

 for $match_result in /*/book/*[contains(.,'linear')]/text()
  return substring($match_result, functx:index-of-match-first($match_result,'linear'), 50)

when applied on the provided XML document (with several non-well-formedness errors corrected):

<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>

produces the expected result:

linear systems linear system analysis

It is a good practice to use the import module directive to import modules from existing libraries of functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文