XQuery:本地函数中的命名空间问题

发布于 2024-10-03 18:01:31 字数 502 浏览 0 评论 0原文

我的本地函数存在以下问题。

以下函数:

declare function local:exp($w as node()) as element()* {
 for $e in ($w/e)
 let $exp:= QName ("myns", "real")
 return 
  element {$exp}{ 
   attribute resource {$e/@lang}
  }
};

生成此 xml:

<real xmlns="myns" resource="eng"/>

真正需要的是:

<myns:real rdf:resource="lang"/>

我怎样才能实现这一目标?

  1. 我该如何解决这个问题?
  2. 如何添加“rdf”作为资源属性的 NS。

提前致谢。

I have the following issue with my local function.

The following function:

declare function local:exp($w as node()) as element()* {
 for $e in ($w/e)
 let $exp:= QName ("myns", "real")
 return 
  element {$exp}{ 
   attribute resource {$e/@lang}
  }
};

generates this xml:

<real xmlns="myns" resource="eng"/>

What really needed is:

<myns:real rdf:resource="lang"/>

How can I achive that?

  1. How can I address the problem?
  2. How can I add "rdf" as NS for resource attribute.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小霸王臭丫头 2024-10-10 18:01:31

您可以将前缀分配给 QName,如下所示:

let $exp:= QName ("urn:my-namespace", "myns:real")

解决此问题的最佳方法可能是在查询中声明这些命名空间并仅通过前缀引用它们:

declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace myns="urn:my-namespace";

declare function local:exp($w as node()) as element()* {
 for $e in $w/e
 return 
  element myns:real { 
   attribute rdf:resource {$e/@lang}
  }
};

请注意,您可以通过使用直接构造函数来简化函数:

declare function local:exp($w as node()) as element()* {
 for $e in $w/e
 return <myns:real rdf:resource="{$e/@lang}" />
};

You can assign the prefix to the QName as so:

let $exp:= QName ("urn:my-namespace", "myns:real")

Probably the best way to solve this is to declare these namespaces in your query and just refer to them by prefix:

declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace myns="urn:my-namespace";

declare function local:exp($w as node()) as element()* {
 for $e in $w/e
 return 
  element myns:real { 
   attribute rdf:resource {$e/@lang}
  }
};

Note that you can simplify your function by using direct constructors:

declare function local:exp($w as node()) as element()* {
 for $e in $w/e
 return <myns:real rdf:resource="{$e/@lang}" />
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文