How to copy / clone Node in Marklogic XQuery

I am writing code that should return a modified XML version of a node without changing the original node in the parent document.

How can I copy / clone a node so that the original context is not associated with / affected by it? I don't want the changes made to this node to change the original node in the parent document, only to the copy returned by my function.

What I'm looking for would be very similar to whatever cts: highlight does inside:

Returns a copy of node , replacing any text that matches the query with the specified expression. You can easily use this function to highlight any text found in the query. Unlike fn: replace and other XQuery string functions that match literal text, cts: match-highlighting each term matching the search, including matched matches or matches with different capitalizations. [marklogic docs> cts: highlight]

+3


source to share


3 answers


The easiest way to create a clone / copy of a node is to use the computed document node constructor :

document{ $doc }

      



If you are cloning a node that is not document-node()

, and you don't want to document-node()

, just clone the original node()

, you can XPath pick which node is cloned from the new one document-node()

:

document{ $foo }/node()

      

+6


source


Just for completeness: usually the standard XQuery Update has copy-modify expressions that explicitly do a copy. Without any changes, it looks like an explicit cloning.

copy $node := $foo
modify ()
return $node

      



I'm not sure if MarkLogic supports this syntax or not. As far as I know, it uses its own function library for updates.

0


source


In-memory XML nodes are not directly modified. Instead, you make the changes you want when you create a new node. If you know XSLT, this might be a good way to do it. If not, you can use an XQuery technique called recursive descent .

0


source







All Articles