S K padala
S K padala

Reputation: 251

How to insert an XML tag using transforms in Marklogic?

I have the xml document and need to add an extra tag to the existing xml using transforms in marklogic. This must not use any xquery.

All the coding must be in javascript.

Here is the code for adding new tag to JSON:

function insertTimestamp(context, params, content)
{
  workaround(context);
  if (context.inputType.search('json') >= 0) {
    var result = content.toObject();
    if (context.acceptTypes) {                 /* read */
      result.readTimestamp = fn.currentDateTime();
    } else {                                   /* write */
      result.writeTimestamp = fn.currentDateTime();
    }
    return result;
  } else {
    /* Pass thru for non-JSON documents */
    return content;
  }
};

exports.transform = insertTimestamp;

In the same way, I need to add a tag to XML(instead JSON).

Upvotes: 0

Views: 127

Answers (2)

Dave Cassel
Dave Cassel

Reputation: 8422

You can work with Server-side JavaScript even if you're using a library module written in XQuery. For instance, you could make use of Ryan Dew's XQuery XML Memory Operations library.

var mem = require('/lib/memory-operations.xqy');

mem.insertChild(...);

Note that the worm-case names you import from XQuery will be accessed as camel-case names in JavaScript.

Upvotes: 2

ehennum
ehennum

Reputation: 7335

As in XQuery, nodes are immutable in server-side JavaScript.

To modify an XML structure, the strategy is to create a new structure by copying the nodes you want to keep, omitting the nodes to delete, and creating new nodes to insert.

In server-side JavaScript, you can use the builder API to create the new XML structure:

http://docs.marklogic.com/guide/jsref/api#id_90865

Upvotes: 0

Related Questions