XQuery TUTORIAL

Multiple XML Output Files Using XQuery Tutorial

Updated: 12 Jan 2022

Introduction

A common requirement of DataDirect XQuery customers is the ability to generate multiple XML output files. 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 from a single XQuery? No need to glue multiple queries together, do external scripting, or write Java or any other proprietary code. Simply a single XQuery!

Generating Multiple XML Output Files

This use case is not new. And not surprising, people are generating multiple XML output files in XSLT since a long time using the xsl:result-document instruction. However, 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 the following 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.

XQuery Output Example

The following XML output 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 documents 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

How to Generate Multiple XML Output Files with XQuery

In this tutorial you learned how easy it is to generate multiple output documents from a single XQuery application. To try it out yourself, download a free trial of DataDirect XQuery today.

Connect any application to any data source anywhere

Explore all DataDirect Connectors

Need additional help with your product?

Get Customer Support