thichxai
thichxai

Reputation: 1147

Marklogic how to loop each node

I try to loop each $node to get element then return string-join() of all elements together in local:function. but it is not working.

XDMP-AS: (err:XPTY0004) local:test-case($value) -- Invalid coercion: "EF127557 Y" as node().

I could not figure out what it means of this error code.

xquery version "1.0-ml";
declare namespace techco="http://techco.com/db/record";
declare namespace meta="http://techco.com/db/record/meta";

declare function local:test-case($nodes as node()*) as node()* {
let $pl := for $node in $nodes
            let $ANumber        := $node/db:Record/meta:Metadata/meta:ANumber
            let $ActiveFlag             := $node/db:Record/meta:Metadata/meta:ActiveFlag
            let $value:=   fn:string-join(($ANumber,$ActiveFlag)," ")                                         
              return $value

return $pl
};

let $query:=cts:and-query((
                    ....
        ))

let $value := cts:search(
                   fn:doc(),
                   $query
)
  return local:test-case($value)

Upvotes: 2

Views: 305

Answers (1)

Tyler Replogle
Tyler Replogle

Reputation: 1339

So I can't test this because of not having example data but this should work now. The problem was your return type was set as node() and needed to be xs:string*. an Invalid coercion: error means the data type its getting is not what it is expecting.

xquery version "1.0-ml";
declare namespace techco="http://techco.com/db/record";
declare namespace meta="http://techco.com/db/record/meta";

declare function local:test-case($nodes as node()*) as xs:string* {
let $pl := for $node in $nodes
            let $ANumber        := $node/db:Record/meta:Metadata/meta:ANumber
            let $ActiveFlag             := $node/db:Record/meta:Metadata/meta:ActiveFlag
            let $value:=   fn:string-join(($ANumber,$ActiveFlag)," ")                                         
              return $value

return $pl
};

let $query:=cts:and-query((
                    ....
        ))

let $value := cts:search(
                   fn:doc(),
                   $query
)
  return local:test-case($value)

Upvotes: 4

Related Questions