tags:

views:

87

answers:

2

I am trying to transform XML file twice with different XSLT files (Two step view). Is it possible to do so?

Example: data.xml -> transformed by first.xsl -> result of first transformation (XML) -> transformed by second.xsl -> result of second transformation (HTML)

+2  A: 

Unfortunately, with standards-compliant XSLT 1.0: no, this is not possible.

In XSLT 2.0, a template's return value may be used as input to another template; so an upgrade to XSLT 2.0 (which is easier to work with on many other fronts as well) would solve this limitation for you.

Another workaround is using the node-set extension function: but, being non-standard, this is obviously not supported everywhere identically: see http://www.xml.com/pub/a/2003/07/16/nodeset.html for details.

Eamon Nerbonne
+1  A: 

In XSLT 2.0 this is supported -- just capture in an <xsl:variable/> the result of the first transformation, then apply templates (possibly with different mode) to the top child (or any other descendents) of the xml document/fragment contained in the xsl:variable.

In XSLT 1.0 one has to use the xxx:node-set() extension, which converts the contents of the xsl:variable (which is of type RTF -- Result Tree Fragment) into a regular XML document/fragment.

This extension-function is quite standardized by EXSLT -- the "most standard" and widely implemented library of XSLT 1.0 extension functions.

Dimitre Novatchev
Though I've never quite understood the need for a "result tree fragment" data type in 1.0. It has bitten me quite often, and I really have no idea why it exists in the first place. Can you shed light on this?
Tomalak
@Tomalak Some people that were in the XSLT 1.0 WG shared that James Clark did this on purpose, in order to prevent XSLT to be used as a programming language ... Of course, this didn't stop the language, only with one simple extension function and libraries such as FXSL, one can do anything in XSLT. Then came XSLT 2.0 and this mistake was corrected.
Dimitre Novatchev