I have a situation where I think I need to daisy chain my xslt transformation (i.e. that output of one xslt transform being input into another). The first transform is rather complex with lots of xsl:choice and ancestor xpaths. My thought is to transform the xml into xml that can then be easily transformed to html.
My question is 'Is this standard practice or am I missing something?'
Thanks in advance.
Stephen
I wouldn't think it was standard practice, in particular since you can transform one XML dialect directly to another.
However, if the processing is complex, splitting it to several steps (applying a different transform in each step) can indeed simplify each step and make sense.
It really depends on the particular situation.
If this is your situation (or may become your situation):
or
Then it makes sense to use a two step transformation.
If this is your situation:
Then consider not two stepping. Instead just perform one transformation.
Performing a chain of transformations is used quite often in XSLT applications, though doing this entirely in XSLT 1.0 requires the use of the vendor-specific
xxx:node-set()
function. In XSLT 2.0 no such extension is needed as the infamous RTF datatype is eliminated there.Here is an example (too-simple to be meaningful, but illustrating completely how this is done):
when this transformation is applied on the following XML document:
the wanted, correct result is produced:
Explanation:
In the first step the XML document is transformed and the result is defined as the value of the variable
$vrtfPass1
. This copies only thenum
elements that have odd value (not even).The
$vrtfPass1
variable, being of type RTF, is not directly usable for XPath expressions so we convert it to a normal tree, using the EXSLT (implemented by most XSLT 1.0 processors) functionext:node-set
and defining another variable --$vPass1
whose value is this tree.We now perform the second transformation in our chain of transformations -- on the result of the first transformation, that is kept as the value of the variable
$vPass1
. Not to mess with the first-pass template, we specify that the new processing should be in a named mode, called "pass2". In this mode the value of anynum
element is multiplied by two.XSLT 2.0 solution (no RTFs):