XQuery creates HTML including images

February 06, 2008 Data & AI

This is the fourth post in the series XQuery generating multiple documents. Today we show 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 SQL Server, but as we will demonstrate this works perfectly against any other database supported by DataDirect XQuery - Oracle, DB2, Sybase, 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.

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.

The following query shows how to generate the main HTML page which references the JPEG files. The idea here is to save the HTML document in the same directory as the JPEG files. The table in our SQL Server database is fairly simple, having 3 columns with the name of the image, a small description and the actual picture.

[cc lang="xquery"]{ "My favourite pictures.",

 

, (: 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 := {$image/image/text()} return (<a href="./{$filename}">{ $image/description, (: create the JPEG file :) ddtek:serialize-to-url($jpeg, concat("C:/test", $filename), "method=binary") },

) }[/cc]

 

In this example we've used JPEG files, but you can apply the outlined technique with any other binary format.

 

digg_skin = 'compact'; digg_url = 'http://www.xml-connection.com/2008/02/xquery-creates-html-including-images.html'

Tech Tags:

Marc Van Cappellen