简体   繁体   中英

XQuery get node without namespace

I have an XML with a namespace:

<namespace:root>
  <namespace:node>
    <namespace:subnode>value</namespace:subnode>
  </namespace:node>
</namespace:root>

I need to perform an XQuery to get, as an output, the same XML or a subnode, without namespace:

<root>
  <node>
    <subnode>value</subnode>
  </node>
</root>

Is there a XQuery syntax for this?

You can use a recursive function for stripping all prefixes from element and attribute names:

declare function local:strip-namespaces($node as node()) as node()* {
  if($node instance of element()) then (
    element { local-name($node) } {
      for $attribute in $node/@*
      return attribute { local-name($attribute) } { $attribute },
      for $child in $node/node()
      return local:strip-namespaces($child)
    }
  ) else (
    $node
  )
};

let $xml :=
  <namespace:root xmlns:namespace='URI' namespace:id='123'>
    <namespace:node>
      <namespace:subnode>value</namespace:subnode>
    </namespace:node>
  </namespace:root>
return local:strip-namespaces($xml)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM