XML Connections

Tuesday, December 4, 2007

XQuery generating multiple XML documents?

A requirement we often see with DataDirect XQuery customers, is the ability to generate multiple results. Imagine a directory with XML files, each of which needs to be transformed and result in a new XML document. Wouldn’t it be great to query the complete directory and generate the corresponding results, all in a single XQuery? No need to glue multiple queries together, do external scripting, write Java or any other proprietary code. Simply a single XQuery!

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>
return
ddtek:serialize-to-url($item,
"ftp://uid:pwd@myftpserver/results/", "")

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,

let $item := <message>XQuery rocks!</message>
return
ddtek:serialize-to-url($item,
"file:///results/output.xml",
"omit-xml-declaration=no,encoding=UTF-16")

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.

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 follows

java -jar C:\ddxq\lib\ddxq.jar C:\xquery\copy-document.xq

In the next days, we’ll show through some real use cases how to take advantage of ddtek:serialize-to-url

Tech Tags:

Labels: ,

0 Comments:

Post a Comment



<< Home