Create the XQuery Transformation Module
If you prefer to work with a Server-Side JavaScript transform function, skip this section and go to Create the JavaScript Transformation Module.
This example module modifies XML input documents by adding an attribute named NEWATTR
. Other input document types pass through the transform unmodified.
In a location other than the sample input data directory, create a file named transform.xqy
with the following contents. For example, copy the following into /space/mlcp/txform/transform.xqy
.
xquery version "1.0-ml"; module namespace example = "http://marklogic.com/example"; (: If the input document is XML, insert @NEWATTR, with the value : specified in the input parameter. If the input document is not : XML, leave it as-is. :) declare function example:transform( $content as map:map, $context as map:map ) as map:map* { let $attr-value := (map:get($context, "transform_param"), "UNDEFINED")[1] let $the-doc := map:get($content, "value") return if (fn:empty($the-doc/element())) then $content else let $root := $the-doc/* return ( map:put($content, "value", document { $root/preceding-sibling::node(), element {fn:name($root)} { attribute { fn:QName("", "NEWATTR") } {$attr-value}, $root/@*, $root/node() }, $root/following-sibling::node() } ), $content ) };