Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Copy/Clone a Node in Marklogic XQuery

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

How can I copy/clone the node so that the original context will not be connected to/affected by it? I don't want changes made to this node to change the original node in the parent document, just to the copy that my function is returning.

What I'm looking for would be very similar to whatever cts:highlight is doing internally:

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

like image 239
Fateh Khalsa Avatar asked Mar 09 '23 20:03

Fateh Khalsa


1 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 a document-node(), and don't want a document-node(), just a clone of the original node(), then you can XPath to select that cloned node from the new document-node():

document{ $foo }/node()
like image 99
Mads Hansen Avatar answered Mar 21 '23 14:03

Mads Hansen