2
votes

I have only just started with MarkLogic and XQuery. I am having a really tough time in modifying the content of one of my XML documents. I just cannot seem to get a change to an element to pick up. Here's my process (I have had to take things back as basic as I could just to try and get it working):

In query console I have one tab open which queries for the contents of one XML doc:

xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
xdmp:document-get("C:/Users/Paul/Documents/MarkLogic/xml/ppl/ppl/jdbc_ppl_3790.xml")

This brings back the document as below

false ... 3790

Victoria Wilson 
</ppl_name>

I now want to update the element using XQuery but it's just not happening. Here's the XQuery:

xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";

let $docxml := 
xdmp:document-get("C:/Users/Paul/Documents/MarkLogic/xml/ppl/ppl/jdbc_ppl_3065.xml")/document/meta/ppl_name
return
  for $node in $docxml/*
  let $target := xdmp:document-get("C:/Users/Paul/Documents/MarkLogic/xml/ppl/ppl/jdbc_ppl_3790.xml")/document/meta/*[fn:name() = fn:name($node)]
  return
  xdmp:node-replace($target, $node)

I am basically looking to replace the ppl_name element in the target (3790) with the ppl_name element from the source (3065).

I run the XQuery - it completes without error (making me thing it has worked) - return value reads your query returned an empty sequence.

I then go back to the same tab as I used in step 1 and re-run the XQuery used in step 1. The doc (3790) comes back but it STILL has Victoria Wilson as the ppl_name.

2

2 Answers

2
votes

The node returned by xdmp:document-get is an in-memory node from a document on the filesystem. It isn't coming from the database. You can't use xdmp:node-replace on in-memory nodes. That's only for database-resident nodes.

You can insert it using xdmp:document-insert. Then it's in the database, and you can access it using doc and update it using xdmp:node-replace. Or you can use in-memory operations to construct a new version with the changes you want.

See What are in memory elements in marklogic? for previous answers to a similar question, and more tips.

1
votes

Here the node returned by xdmp:document-get is an in-memory node If your working with in memory elements import the following module

import module namespace mem = "http://xqdev.com/in-mem-update" at "/MarkLogic/appservices/utils/in-mem-update.xqy";

Instead of using xdmp:node-replace you can use mem:node-replace(<x/>, <y/>)