代码之家  ›  专栏  ›  技术社区  ›  user3111525

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

  •  0
  • user3111525  · 技术社区  · 14 年前

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

    以下功能:

    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。

    事先谢谢。

    1 回复  |  直到 14 年前
        1
  •  1
  •   Oliver Hallam    14 年前

    您可以将前缀指定给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}" />
    };