Loading TOC...

MarkLogic 12 Product Documentation
op:transform-doc

op:transform-doc(
   $transformDocCol as item(),
   $transformDef as map:map
) as map:map

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. The column can be named with a string or a column function such as op:col, op:view-col, or op:schema-col, or constructed from an expression with the op:as function.
$transformDef The 'kind' key of the schema map must be 'mjs'(the default), or 'xslt'. The 'path' key specifies the main module in the modules database. The 'params' key specifies parameter values passed to the main module. (refer to xdmp:invoke and xdmp:xslt-invoke).

See Also

Example

xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

op:from-doc-uris(("/test/test/0.json", "/test/test/1.json"), "view")
=>op:join-doc-cols(op:doc-cols("view"), op:view-col("view", "uri"))
=>op:transform-doc(op:view-col("view", "doc"), map:entry("path", "/transformDoc-test.mjs")=>map:with("kind", "mjs")=>map:with("params", map:entry("myParam", "my new content")))
=>op:order-by(op:view-col("view", "uri"))
=>op:result()

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

Example

xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";
declare option xdmp:update "true";
let $rows := (
  map:entry("id", 1) => map:with("doc", xdmp:unquote('{"doc":1}',(),"format-json"))=> map:with("uri", "/optic/update/write11.json"),
  map:entry("id", 2) => map:with("doc", xdmp:unquote('{"doc":2}',(),"format-json"))=> map:with("uri", "/optic/update/write12.json"),
  map:entry("id", 3) => map:with("doc", xdmp:unquote('{"doc":3}',(),"format-json"))=> map:with("uri", "/optic/update/write13.json"))
let $colTypes := (
  map:entry("column", "id") => map:with("type", "integer")=> map:with("nullable", false()),
  map:entry("column", "doc") => map:with("type", "none")=> map:with("nullable", false()),
  map:entry("column", "uri") => map:with("type", "string")=> map:with("nullable", false())
)
let $fromParam := op:from-param("bindings", "", $colTypes)
let $fromLiteral := op:from-literals((
  map:entry("id",1)=>map:with("val","patch1"),
  map:entry("id",2)=>map:with("val","patch2")
  ))
return $fromParam
  =>op:join-inner($fromLiteral)
  =>op:order-by("id")
  =>op:transform-doc(op:col("doc"), map:entry("path", "/transformDoc-test-two-params.mjs")=>map:with("kind", "mjs")
                   =>map:with("params", map:entry("patch1",op:col("id"))=>map:with("patch2",op:col("val"))))
  =>op:write(map:entry("uri","uri")=>map:with("doc","doc"))
  =>op:result("object", map:entry("bindings", $rows))

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

Example

xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";
declare option xdmp:update "true";
let $fromDocUris := op:from-doc-uris(cts:document-query(("/optic/update/write7.xml", "/optic/update/write8.xml", "/optic/update/write9.xml")))
let $fromLiteral := op:from-literals((
  map:entry("uri","/optic/update/write7.xml")=>map:with("val","patch1"),
  map:entry("uri","/optic/update/write8.xml")=>map:with("val","patch2")
  ))
return $fromDocUris
=>op:join-doc(op:col("doc"), op:col("uri"))
=>op:join-inner($fromLiteral)
=>op:order-by("uri")
=>op:transform-doc(op:col("doc"), map:entry("path", "/transformDoc-test.xslt")=>map:with("kind", "xslt")
                   =>map:with("params", map:entry("myParam",op:col("val"))))
=>op:write(map:entry("uri","uri")=>map:with("doc","doc"))
=>op:result()
  

Example

xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

let $docDescriptors := (
    map:entry("uri", '/test/fromDocUris/0.json')
      =>map:with("doc", xdmp:unquote('{"doc":"doc0"}',(),"format-json")),
    map:entry("uri", '/test/fromDocUris/1.json')
      =>map:with("doc", xdmp:unquote('{"doc":"doc1"}',(),"format-json"))
  )
let $fromLiteral := op:from-literals((
  map:entry("uri","/test/fromDocUris/0.json")=>map:with("id",0)=>map:with("val","content0"),
  map:entry("uri","/test/fromDocUris/1.json")=>map:with("id",1)=>map:with("val","content1"),
  map:entry("uri","/test/fromDocUris/5.json")=>map:with("id",5)=>map:with("val","content5")
  ), "right")
return op:from-doc-descriptors($docDescriptors, "left")
=>op:join-inner($fromLiteral, op:on(op:view-col("left","uri"), op:view-col("right","uri")))
=>op:transform-doc(op:view-col("left","doc"), map:entry("path", "/transformDoc-test-two-params-array.mjs")=>map:with("kind", "mjs")
                    =>map:with("params", map:entry("patch1",op:view-col("left","uri"))=>map:with("patch2",op:view-col("right","val"))))
=>op:order-by(op:view-col("left","uri"))
=>op:result()

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

Example

xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

let $docDescriptors := (
    map:entry("uri", '/test/fromDocUris/0.json')
      =>map:with("doc", xdmp:unquote('{"doc":"doc0"}',(),"format-json")),
    map:entry("uri", '/test/fromDocUris/1.json')
      =>map:with("doc", xdmp:unquote('{"doc":"doc1"}',(),"format-json"))
  )
let $fromLiteral := op:from-literals((
  map:entry("uri","/test/fromDocUris/0.json")=>map:with("id",0)=>map:with("val","content0"),
  map:entry("uri","/test/fromDocUris/1.json")=>map:with("id",1)=>map:with("val","content1"),
  map:entry("uri","/test/fromDocUris/5.json")=>map:with("id",5)=>map:with("val","content5")
  ), "right")
return op:from-doc-descriptors($docDescriptors, "left")
=>op:join-inner($fromLiteral, op:on(op:view-col("left","uri"), op:view-col("right","uri")))
=>op:transform-doc(op:view-col("left","doc"), map:entry("path", "/transformDoc-test-two-params-sequence.mjs")=>map:with("kind", "mjs")
                    =>map:with("params", map:entry("patch1",op:view-col("left","uri"))=>map:with("patch2",op:view-col("right","val"))))
=>op:order-by(op:view-col("left","uri"))
=>op: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.