thichxai
thichxai

Reputation: 1133

Marklogic How to save documents into single xml in file system

The Qconsole return multiple document nodes. But not know how to save all document nodes to file system into a single xml file. I don't know why it only save the last document. ;-(( Thanks in advance.

my sample source code.

(:validate condition and concate values:)
declare function local:ifx($mnem,$val) as node()* {
   if(fn:exists($val)) 
   then xdmp:value(fn:concat("<meta name=""{$mnem}"" content=""",$val,""" />")) 
else ()
};

(:Loop and concat values:)
declare function local:forx($mnem,$vals) as node()* {
   for $val in $vals
       return xdmp:value(fn:concat("<meta name=""{$mnem}"" content=""",$val,""" />"))
};


declare function local:ETL($nodes as node()*) as node()*{

  for $n in $nodes
      let $v_id := $n/rec:record/meta:Metadata/meta:id
      let $v_ba := for $elem in $n/rec:record/meta:Metadata/meta:fundingSource
                      return if (fn:string-length($elem/meta:BugetCode/meta:code)= 1)
                            then fn:concat("0",$elem/meta:BugetCode/meta:code)
                            else $elem/meta:BugetCode/meta:code
  return
     <record url="dbfeed.iadb" mimetype="text/html" last-modified="NA">
          <metadata>
              {local:ifx("id",$v_id)}
              {local:forx("ba",$v_ba)}
          </metadata> 
      </record>
     )

};

(:find all documents within a year:)
let $docs := cts:search(
fn:doc(),
    cts:and-query((
                  cts:element-value-query(xs:QName("meta:Collection"),"EDS"),
                  cts:field-range-query("rd",">=","2016-01-01"),
                  cts:field-range-query("rd","<","2017-01-01")
                  ))
)

let $XML2016 :=
       for $i in (local:uredetl($docs))
       return $i

 return xdmp:save("/output/all-data-2016.xml") ,$XML2016) 

Upvotes: 1

Views: 423

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66723

The variable $XML2016 has a sequence of items, but the second parameter for xdmp:save() expects a single node().

In MarkLogic, when your XQuery version is declared as 1.0-ml, function mapping is enabled by default. This means that the xdmp:save() function will be invoked once for each of the documents in the sequence. After all of those saves, the last document is the only one that you see.

If you disabled function mapping:

declare option xdmp:mapping "false";

and then executed your code, an XDMP-ARGTYPE error would be thrown, complaining that you have provided a sequence of nodes to the xdmp:save() function.

If you have multiple items that you want to save to the same file, you need to combine them first and then invoke save once with a with a single node, or save each with their own distinct filename, or maybe look to use zip:create() instead.

Upvotes: 2

Related Questions