Loading TOC...

ModifyPlan.prototype.transformDoc

ModifyPlan.prototype.transformDoc(
   transformDocCol as String,
   transformDef as Object
) as ModifyPlan

Summary

This function applies a transformation to a column that contains documents. This transform module needs to be stored in the modules database.

Parameters
transformDocCol This is the column which contains the document to transform. This can be a string of the column name or an op.col. Use op.viewCol or op.schemaCol if you need to identify columns in the two views that have the same column name.
transformDef This is an object. The 'kind' property of the schema object must be 'mjs'(the default), or 'xslt'. The 'path' property specifies the main module in the modules database. The 'params' property specifies parameter values passed to the main module. (refer to xdmp.invoke and xdmp.xsltInvoke).

See Also

Example

const op = require('/MarkLogic/optic');
op.fromDocUris(['/test/0.json', '/test/1.json'], 'view')
  .joinDocCols(op.docCols('view'), op.viewCol('view', 'uri'))
  .transformDoc(op.viewCol('view','doc'), {"path":"/transformDoc-test.mjs", kind:"mjs", params:{myParam:'my new content'}})
  .orderBy('uri')
  .result()

/* transformDoc-test.mjs
const result = {
    "hello": "world",
    "yourParam": external.myParam,
    "theDoc": external.doc
}
result
*/
  

Example

declareUpdate();
const op = require('/MarkLogic/optic');
const rows = [
    {id:1, doc:{"doc":1}, uri:'/optic/update/write11.json'},
    {id:2, doc:{"doc":2}, uri:'/optic/update/write12.json'},
    {id:3, doc:{"doc":3}, uri:'/optic/update/write13.json'}
    ];
const colTypes = [
    {"column":"id",  "type":"integer", "nullable":false},
    {"column":"doc", "type":"none",  "nullable":false},
    {"column":"uri", "type":"string",  "nullable":false},
    ];
const fromParam = op.fromParam('bindings', null, colTypes);
const fromLiteral = op.fromLiterals([
        {id:1, val:'patch1'},
        {id:2, val:'patch2'}
        ])
fromParam
  .joinInner(fromLiteral)
  .orderBy('id')
  .transformDoc('doc', {"path":"/transformDoc-test-two-params.mjs", kind:"mjs",
                  params:{patch1:op.col('id'), patch2:op.col('val')}})
  .write({uri:'uri', doc:'doc'})
  .result('object',{'bindings': rows});

/* transformDoc-test-two-params.mjs
const result = {"hello": "world",
"Patch1": external.patch1,
"Patch2": external.patch2,
"theDoc": external.doc}
result
*/
  

Example

const op = require('/MarkLogic/optic');
const fromDocUris = op.fromDocUris(cts.documentQuery(["/optic/update/write7.xml", "/optic/update/write8.xml", "/optic/update/write9.xml"]),
                                  'left')
const fromLiteral = op.fromLiterals([
        {uri:"/optic/update/write7.xml", val:'patch1'},
        {uri:"/optic/update/write8.xml", val:'patch2'}
        ],'right')
fromDocUris
  .joinDoc(op.viewCol('left',"doc"), op.viewCol('left',"uri"))
  .joinInner(fromLiteral)
  .where(op.eq(op.viewCol('left',"uri"), op.viewCol('right',"uri")))
  .orderBy(op.viewCol('left','uri'))
  .transformDoc(op.viewCol('left',"doc"), {"path":"/transformDoc-test.xslt", "kind":"xslt", params:{myParam:op.viewCol('right','val')}})
  ..result()
  

Example

const op = require('/MarkLogic/optic');
const fromDocUris = op.fromDocDescriptors([
  {uri:'/test/fromDocUris/0.json', doc:{'doc':'doc0'}},
  {uri:'/test/fromDocUris/1.json', doc:{'doc':'doc1'}}], 'left')
const fromLiteral = op.fromLiterals([
        {uri:'/test/fromDocUris/0.json', id:0, val:'content0'},
        {uri:'/test/fromDocUris/1.json', id:1, val:'content1'},
        {uri:'/test/fromDocUris/5.json', id:5, val:'content5'}
        ], 'right')
fromDocUris
  .joinInner(fromLiteral, op.on(op.viewCol('left','uri'), op.viewCol('right','uri')))
  .transformDoc(op.viewCol('left', 'doc'), {"path":"/transformDoc-test-two-params-array.mjs", kind:"mjs",
                    params:{patch1:op.viewCol('left','uri'), patch2:op.viewCol('right','val')}})
  .orderBy(op.viewCol('left','uri'))
  .result()

/* transformDoc-test-two-params-array.mjs
const result = [
{"Patch1": external.patch1,"theDoc": external.doc},
{"Patch2": external.patch2,"theDoc": external.doc}
]
result
*/
  

Example

const op = require('/MarkLogic/optic');
const fromDocUris = op.fromDocDescriptors([
  {uri:'/test/fromDocUris/0.json', doc:{'doc':'doc0'}},
  {uri:'/test/fromDocUris/1.json', doc:{'doc':'doc1'}}], 'left')
const fromLiteral = op.fromLiterals([
        {uri:'/test/fromDocUris/0.json', id:0, val:'content0'},
        {uri:'/test/fromDocUris/1.json', id:1, val:'content1'},
        {uri:'/test/fromDocUris/5.json', id:5, val:'content5'}
        ], 'right')
fromDocUris
.joinInner(fromLiteral, op.on(op.viewCol('left','uri'), op.viewCol('right','uri')))
.transformDoc(op.viewCol('left', 'doc'), {"path":"/transformDoc-test-two-params-sequence.mjs", kind:"mjs",
                  params:{patch1:op.viewCol('left','uri'), patch2:op.viewCol('right','val')}})
.orderBy(op.viewCol('left','uri'))
.result()

/* transformDoc-test-two-params-sequence.mjs
const result1 = {"hello": "world",
"Patch1": external.patch1,
"theDoc": external.doc}
const result2 = {"hello": "world",
"Patch2": external.patch2,
"theDoc": external.doc}
Sequence.from([result1,result2])
*/
  

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.