Search code examples
xquerysequencemarklogic

Xquery - what is wrong here


I would like the node replace to act on the $person variable. What do I need to change?

The following code should change the name of the people in the sequence to X.

declare function local:ChangeName($person)
{
  xdmp:node-replace($person//Name/text, text { "X" } )

  <p>{$person}</p>
};

let $b := <Person>
      <Name>B</Name>
      <IsAnnoying>No</IsAnnoying>
  </Person>

let $j := <Person>
      <Name>J</Name>
      <IsAnnoying>No</IsAnnoying>
  </Person>

let $people := ($b, $j)

return $people ! local:ChangeName(.)

Solution

  • xdmp:node-replace() only operates on persisted documents, not on in-memory documents.

    Your local:ChangeName() function could construct the Person and Name elements but copying the IsAnnoying element, as in:

    declare function local:ChangeName($person)
    {
      <p>
      <Person>
      <Name>X</Name>
      {$person//IsAnnoying}
      </Person>
      </p>
    };
    

    For more complex transformations, consider a recursive typeswitch or XSLT transform.