0

I am having a code where i am using mem:node-replace(). The code is as below-

let $a := 
  for $i in $res
  let $uri := fn:base-uri($i)
  let $doc := fn:doc($uri)
  return if(fn:exists($doc) eq fn:true()) 
    then (
      (
        mem:node-replace($doc//*:NODE1,<NODE1>{doc($id)//*:NODE1}</NODE1>),
        mem:node-replace($doc//*:NODE2,<NODE2>{$curr_date}</NODE2>)
      ) 
    ) 
    else () 
return $a

I can able to see the value is getting replaced in the LOG but it is not reflecting into the database. How i am supposed to commit/save this node-replace in my database ?

Any Suggestions ?

2 Answers 2

6

The mem:replace function only updates the node in memory. You have created an in-memory copy of the document and made changes to it, but not propagated those changes in the database. This is similar to updating a variable passed by value instead of by pointer.

Following your in-memory updates, you need to insert the updated document back into the database.

let $a := for $i in $res
let $uri := fn:base-uri($i)
let $doc := fn:doc($uri)
let $_update := 
  if (fn:exists($doc) eq fn:true())
  then (mem:node-replace($doc//*:NODE1, <NODE1>{doc($id)//*:NODE1}</NODE1>), 
    mem:node-replace($doc//*:NODE2, <NODE2>{$curr_date}</NODE2>))    
  else ()
return xdmp:document-insert($uri, $doc)
Sign up to request clarification or add additional context in comments.

Comments

4

If you need to update a node I would use xdmp:node-replace which replaces the node on disk.

Here's an example from the documentation: https://docs.marklogic.com/xdmp:node-replace

(: create an XML document :)
xdmp:document-insert("/example.xml",
    <a><b>bbb</b></a>);

(: replace the b node with a c node :)
xdmp:node-replace(doc("/example.xml")/a/b, <c>ccc</c>);

(: look at the new document :)
fn:doc("/example.xml")

2 Comments

I can't use xdmp here as it gives me conflicting updates error.
Sorry about that, I didn't scroll far enough to the right to see the multiple updates. Since you are performing multiple transformations to the same node wst's answer is a better answer to your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.