XQuery generating multiple XML documents?
The use case is not new. And not surprising, people are doing this in XSLT since a long time through the xsl:result-document instruction. Despite the library of almost 200 built-in functions in XQuery, there is not such a function in XQuery 1.0.
The good news is that DataDirect XQuery 3.1 adds out-of-the-box support to serialize query results in a file.
ddtek:serialize-to-url(
$items as item()*,
$url as xs:string,
$options as xs:string)
ddtek:serialize-to-url() has three arguments, first the data to be serialized, second a URL specifying the file to be saved and the last argument tweaks the serialization process.
The following example creates a output.xml file in the c:\results directory,
let $item := <message>XQuery rocks!</message>
return
ddtek:serialize-to-url($item,
"file:///results/output.xml","")
As you see there is no need at all to write any Java code, this can be executed from within your favorite XQuery editor or using the DataDirect XQuery command line utility.
As a URL specifies the output location, you can for example easily upload the result to an FTP server,
let $item := <message>XQuery rocks!</message>Finally, the serialization of the results can be tweaked. Here we encode the XML output as UTF-16 and specify to include an XML declaration,
return
ddtek:serialize-to-url($item,
"ftp://uid:pwd@myftpserver/results/", "")
let $item := <message>XQuery rocks!</message>Let's look at a more concrete example, where all XML document are copied from one directory to another. in this query we use DataDirect XQuery's capability to query a complete directory through fn:collection.
return
ddtek:serialize-to-url($item,
"file:///results/output.xml",
"omit-xml-declaration=no,encoding=UTF-16")
declare function local:get-file-name($document-uri as xs:string){
tokenize($document-uri, "/")[last()]
};
for $doc in fn:collection("file:///C:/input?select=*.xml")
let $filename := concat("file:///C:/output/",
local:get-file-name(document-uri($doc)))
return
ddtek:serialize-to-url($doc, $filename, "")Assuming this XQuery is saved as C:\xquery\copy-document.xq, it can be executed from the command line as followsjava -jar C:\ddxq\lib\ddxq.jar C:\xquery\copy-document.xqIn the next days, we’ll show through some real use cases how to take advantage of ddtek:serialize-to-url

0 Comments:
Post a Comment
<< Home