Skip to main content

Using MarkLogic Content Pump (mlcp)

Create the JavaScript transform module

If you prefer to work with an XQuery transform function, skip this section and go to Create the XQuery transform module.

This example module modifies JSON input documents by adding an attribute named NEWPROP. Other input document types pass through the transform unmodified.

In a location other than the sample input data directory, create a file named transform.sjs with the following contents. For example, copy the following into /space/mlcp/txform/transform.sjs.

// Add a property named "NEWPROP" to any JSON input document.
// Otherwise, input passes through unchanged.
function addProp(content, context)
{
  var propVal = (context.transform_param == undefined)
                 ? "UNDEFINED" : context.transform_param;
  var docType = xdmp.nodeKind(content.value);
  if (xdmp.nodeKind(content.value) == 'document' &&
      content.value.documentFormat == 'JSON') {
    // Convert input to mutable object and add new property
    var newDoc = content.value.toObject();
    newDoc.NEWPROP = propVal;
    // Convert result back into a document
    content.value = xdmp.unquote(xdmp.quote(newDoc));
  }
  return content;
};
exports.transform = addProp;