Friday, 10 June 2011

Using collection() with saxon

Often you need to get a load of xml documents in scope of your transformation, a bit like an XML Database.

Of course you can load each document to one or more variables, either at run-time or at the begining of the transformation. Alternatively you might want to consider the use of collection() to load documents from a local base URI, and voila all documents get parsed and are stored as a document() sequence in the scope of a variable.

Saxon also allows you to define a particular file extention to filter the files in that collection, and activate a recursiev behaviour.

<!-- uri of folder of input document -->
<xsl:variable name="baseUri" select="replace(document-uri(/), '(.*)/.*$', '$1')"/>

<!-- all schema documents in the base URI -->
<xsl:variable name="schemas" select="collection(concat($baseUri, '?select=*.xsd;recurse=yes'))/xs:schema"/>

the code above resolves the input document URI, and then loads all *.xsd files recursively from the input document's hosting folder.

The variable schemas now holds all *.xsd files as a sequence of xs:schema elements, so you can easily process the fileset just like the input document, or any other document loaded with the doc() function.

Unfortunately, it looks like it's not possible to use xsl:key with documents loaded externally, as this would certainly speedup parts of the transform, and could easily represent a low-key equivalent to indexes in an XML Database.