XQuery generating multiple XML documents?

XQuery generating multiple XML documents?

December 04, 2007 0 Comments

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.

[cc lang="xquery"]ddtek:serialize-to-url( $items as item()*, $url as xs:string, $options as xs:string)[/cc]

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,

[cc lang="xquery"]let $item := <message>XQuery rocks!</message> return ddtek:serialize-to-url($item, "file:///results/output.xml","")[/cc]

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,

[cc lang="xquery"]let $item := <message>XQuery rocks!</message> return ddtek:serialize-to-url($item, "ftp://uid:pwd@myftpserver/results/", "")[/cc]

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,

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


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.

[cc lang="xquery"]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, "")[/cc]


Assuming this XQuery is saved as C:xquerycopy-document.xq, it can be executed from the command line as follows


[cc lang="xquery"]java -jar C:ddxqlibddxq.jar C:xquerycopy-document.xq[/cc]


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


digg_skin = 'compact'; digg_url = 'http://www.xml-connection.com/2007/12/xquery-generating-multiple-xml.html';

Tech Tags:

Marc Van Cappellen

View all posts from Marc Van Cappellen on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments are disabled in preview mode.
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

More From Progress
2020 Progress Data Connectivity Report
2020 Progress Data Connectivity Report
Read More
Getting Ahead of the Hybrid Data Curve
Read More
570x321__Top 7 Considerations Before Choosing a Chatbot for Your Enterprise
Top 7 Considerations Before Choosing a Chatbot for Your Enterprise
Read More