Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying namespaces in XQuery

Tags:

xquery

I'm trying to construct a new element based on an old one, including copying in its children. To prevent each child from repeating the namespace definitions for each namespace in scope, I'm interested in copying the namespace declarations as well.

Namespace declarations aren't picked up as regular attributes through $element/@*.

If I restrict my code to only work with XQuery 3.0, I can do the following:

<new-element>
  {
    for $ns-prefix in in-scope-prefixes($element)
    let $ns-uri := namespace-uri-for-prefix($ns-prefix, $element)
    return namespace { $ns-prefix } { $ns-uri }
  }
  ...
</new-element>

Is there a better way?


To clarify the problem a bit -- my original document looks like so:

<root xmlns:stuff="...">
  <child name="foo"/>
  <child name="bar"/>
</root>

When I copy children into a new document, I get the following:

<new-element>
  <child xmlns:stuff="..." name="foo"/>
  <child xmlns:stuff="..." name="bar"/>
</new-element>

...whereas it would make more sense to simply attach the xmlns:stuff declaration to <new-element>.

like image 991
Charles Duffy Avatar asked May 11 '26 15:05

Charles Duffy


1 Answers

If you permit use of XQuery Update, you could use a transform expression (copy/modify/return) to copy the node, then modify the name.

like image 82
Tim Mills Avatar answered May 14 '26 14:05

Tim Mills



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!