Example Implementation
The following example adds an attribute to incoming XML documents and leaves non-XML documents unmodified. The attribute value is specified on the mlcp command line, using the -transform_param
option.
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 ) };
For an end-to-end example of using this transform, see Example: Server-Side Content Transformation.