Created
November 9, 2017 21:57
-
-
Save ableasdale/32dd4667f02e31dbc8c9b98de3757926 to your computer and use it in GitHub Desktop.
MarkLogic MLCP: Transforming a source document with a transform module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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