我的本地函数存在以下问题。
以下功能:
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"/>
我怎样才能做到这一点?
提前致谢。
最佳答案
您可以将前缀分配给 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}" />
};
关于namespaces - XQuery:本地函数中的命名空间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4245995/