简体   繁体   中英

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).

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM