XQuery/XQuery and XSLT

Motivation
You want to create a RESTful web service that executes an XSLT transform on an XML document.

Method
XQuery is superior to XSLT in many ways. XQuery is designed to be brief and concise programming language that interleaves XML and functional language statements. Therefore, XQuery programs are usually much smaller than XSLT. XQuery processors are also designed to use indexes so that XQueries over large data sets can run quickly. But unfortunately there are still some times when you must use XSLT. One example of this is in-browser transforms. The eXist database comes with an XQuery function that allows you to transform an XML file using XSLT.

Creating an XSLT service
eXist includes a function to call an XSLT transform is the following:

transform:transform($input as node?, $stylesheet as item, $params as node?) node?

where: $input is the node tree to be transformed $stylesheet is either a URI or a node to be transformed. If it is an URI, it can either point to an external location or to an XSL stored in the db by using the 'xmldb:' scheme. $params are the optional XSLT name/value parameters with the following structure: 

The result is zero or one nodes. The namespace of the transform module is http://exist-db.org/xquery/transform.

The transform:transform function can be used to provide a service which accepts the url of an XML file, the url of an XSLT script and any other parameters which are passed to the stylesheet.

Currently output is text/html.

Checking XSLT Version
The following XSLT is useful for checking what version of XSLT you are running.

Form-based search
In this example, an XML file on one host is transformed by a XSLT script on another. The XSLT script defines a form to allow the use to select a subset of the entries in the XML file, followed by the search results, if any.
 * 1) Stylesheet
 * 2) Data
 * 3) Search Whisky data

A sequence diagram describes the interaction involved:

Sequence Diagram

Page Scraping Example
see ../Page scraping and Yahoo Weather/

Using XSLT Imports
XSLT allows you to call a stylesheet that imports a common library of other XSLT templates. But not all of the XSLT import path statements will work within eXist. In the following example we will use a XSLT style sheet that imports another style sheet. The following assumes you have a collection called /db/test/xslt and all the files are placed in that collection.

XQuery XSLT Test Program
return Demonstration of running XSLT within an XQuery Demonstration of running XSLT within an XQuery { transform:transform($input, doc("/db/test/xslt/style.xsl"), ) }

Top-Level Style.xsl
In the second line, the following imports do work as expected:

But you will note that using the following does not work:

XForms Example
You can also create a simple XForms example that serves as a front end to this script. See the XRX wikibook for an example of this XForms front end.

  XForms Test Program  Name:  

let $serialization-options := 'method=xml media-type=text/xml omit-xml-declaration=yes indent=no'

let $params :=    

return transform:transform($form, $transform, $params, $serialization-options)

Caching Management
By default, once a document has been transformed it resides in the cache. This is very good for performance reasons if a file needs to be retransformed but sometimes if the source file changes the transform needs to be rerun.

You can disable caching by changing the configuration file. In the file conf.xml change the @caching value from yes to no.:



http://demo.exist-db.org/exist/xquery.xml#N10375