Skip to content

Instantly share code, notes, and snippets.

@ableasdale
Created November 9, 2017 21:57
Show Gist options
  • Save ableasdale/32dd4667f02e31dbc8c9b98de3757926 to your computer and use it in GitHub Desktop.
Save ableasdale/32dd4667f02e31dbc8c9b98de3757926 to your computer and use it in GitHub Desktop.
MarkLogic MLCP: Transforming a source document with a transform module
<Root>
<Batch>
<Documents>
<Document>
<Tag id = "elem1">value1</Tag>
<Tag id = "elem2">value2</Tag>
</Document>
<Document>
<Tag id = "elem1">value1</Tag>
<Tag id = "elem2">value2</Tag>
</Document>
<Document>
<Tag id = "elem1">value1</Tag>
<Tag id = "elem2">value2</Tag>
</Document>
<Document>
<Tag id = "elem1">value1</Tag>
<Tag id = "elem2">value2</Tag>
</Document>
<Document>
<Tag id = "elem1">value1</Tag>
<Tag id = "elem2">value2</Tag>
</Document>
</Documents>
</Batch>
</Root>
./mlcp.sh import -host localhost -port 8000 -username admin -database 19417 \
-password admin -mode local -input_file_path content.xml \
-input_file_type aggregates -aggregate_record_element Document \
-output_uri_prefix /document/ -output_uri_suffix .xml \
-transform_module transform.xqy -transform_namespace "http://marklogic.com/example"
xquery version "1.0-ml";
module namespace example = "http://marklogic.com/example";
declare function example:copy($input as item()*) as item()* {
for $node in $input
return
typeswitch($node)
case element()
return
if (fn:node-name($node) eq xs:QName("Tag"))
then(element {fn:data($node/@id)} {fn:data($node)})
else (
element {name($node)} {
(: output each attribute in this element :)
for $att in $node/@*
return
attribute {name($att)} {$att}
,
(: output all the sub-elements of this element recursively :)
for $child in $node
return example:copy($child/node())
}
)
(: otherwise pass it through. Used for text(), comments, and PIs :)
default return $node
};
declare function example:transform($content as map:map,$context as map:map) as map:map* {
let $the-doc := map:get($content, "value")
return
if (fn:empty($the-doc/element()))
then $content
else map:put($content, "value", document {example:copy($the-doc/*)}), $content
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment