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

时间:2010-11-22 13:42:57

标签: namespaces xquery

我的本​​地功能存在以下问题。

以下功能:

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。
  3. 提前致谢。

1 个答案:

答案 0 :(得分:1)

您可以将前缀分配给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}" />
};
相关问题