Using XQuery to Produce HTML & Images Example Tutorial

Updated: 07 Apr 2022


Need to produce HTML and image output from XQuery? The following tutorial explains how to create an HTML document referencing JPEG files through XQuery. The DataDirect XQuery built-in function ddtek:serialize-to-url is used to save the JPEG images.

The idea is that all the data, including the images, are stored in a relational database. The queries in this post have been tested with DataDirect XQuery against MySQL, etc.

XQuery is known to query and produce XML, but how can it be used to produce images, JPEG files in this particular example? This is another example where the DataDirect XML Converters come to rescue.

An XQuery HTML Example with Images

The JPEG images are stored in our database, and are retrieved by DataDirect XQuery as xs:hexbinary data. Subsequently the DataDirect XML Converters are used to convert the xs:hexbinary into a binary stream, which is saved as a JPEG file.

"My favourite pictures.",<p/>,
(: loop over all images in the database:)
for $image in collection("images")/images
(: the file name of the JPEG file :)
let $filename := concat($image/name, ".jpg")
(: create some virtual XML, which will be
processed by the XML Converters :)
let $jpeg := <jpeg>{$image/image/text()}</jpeg>
(<a href="./{$filename}">{
(: create the JPEG file :)
concat("C:/test", $filename),
Generating HTML from XQuery

In this example we've used JPEG files, but you can apply the outlined technique with any other binary format, and so in a nutshell, DataDirect XQuery provides an easy and scalable solution for generating HTML and images from XQuery. Try it out for yourself today by downloading a free trial.

Connect any application to any data source anywhere

Explore all DataDirect Connectors

Need additional help with your product?

Get Customer Support