xquerymarklogicmarklogic-8marklogic-9

How to replace the $content value in transformation?


I need to replace the $content value in my transformation.

xquery version "1.0-ml";

module namespace test =
  "http://marklogic.com/rest-api/transform/security-load";

declare function test:transform(
    $context as map:map,
    $params as map:map,
    $content as document-node()
) as document-node()
{       
        let $jsonObj := xdmp:from-json($content)
        let $inputval := "fname,lname"
        let $entity :="holidayDate"
        let $domain :="referenceData"


        let $uri := xdmp:apply(
            xdmp:function(xs:QName("createUri"), "/wdsUtils.sjs"),
            $jsonObj,
            $inputval)


        let $root := xdmp:apply(
            xdmp:function(xs:QName("addMetaData"), "/wdsUtils.sjs"),
            $entity,
            $domain,
            $jsonObj)

        let $output := $root    

        return map:put($context,"uri",$uri),
        document { $output }
};

I need to change the $content value with $root value and return it. I was trying to return $root directly but it didn't work, I was getting invalid document error.

enter image description here


Solution

  • The problem is that $output is scoped to the FLWOR statement, but you're referring to it outside of that statement. See the parentheses in the return below.

    xquery version "1.0-ml";
    
    module namespace test = "http://marklogic.com/rest-api/transform/security-load";
    
    declare function test:transform(
        $context as map:map,
        $params as map:map,
        $content as document-node()
    ) as document-node()
    {       
        let $jsonObj := xdmp:from-json($content)
        let $inputval := "fname,lname"
        let $entity :="holidayDate"
        let $domain :="referenceData"
    
        let $uri := 
            xdmp:apply(
                xdmp:function(xs:QName("createUri"), "/wdsUtils.sjs"),
                $jsonObj,
                $inputval
            )
    
        let $root := 
            xdmp:apply(
                xdmp:function(xs:QName("addMetaData"), "/wdsUtils.sjs"),
                $entity,
                $domain,
                $jsonObj
            )
    
        let $output := $root    
    
        return (
            map:put($context,"uri",$uri),
            document { $output }
        )
    };