RE: [xsl] xsl, and different xml versions

Subject: RE: [xsl] xsl, and different xml versions
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 15 Apr 2005 09:43:31 +0100
> Hi all
> 
> I have a problem and I am unsure which approach to take in order to 
> solve it. The problem is this:
> 
> The root element of the xml I am transforming with xsl will have a 
> version number in it, indicating which version of the xsl i want to 
> transform it with. By that, I mean I have 2 style sheets, 
> both operating 
> on similer xml. I though I may be able to do this by xsl:import,

The stylesheet is always built and compiled before looking at the contents
of the source document, so this approach is doomed to fail.

You have two choices. 

First approach is to combine the two stylesheets into one, distinguishing
the two cases by use of modes.

Second approach is to have a piece of logic that examines the source
document and decides which of two stylesheets to apply based on the content
of the document.

I think the second solution is probably cleaner (more modular), though it
may mean writing a bit of logic in a language other than XSLT. (If you're
doing a lot of this kind of thing, I would thoroughly recommend using a
pipeline processing language such as Orbeon's XPL, but that would be OTT for
this problem on its own.) Depending on your environment you could write the
control logic in Javascript or Java. You could also do it entirely with XSLT
using Saxon's next-in-chain extension:

<xsl:template match="/">
<xsl:choose xmlns:saxon="http://saxon.sf.net/";>
  <xsl:when test="*/@version='1'">
    <xsl:result-document saxon:next-in-chain="stylesheet1.xsl">
       <xsl:copy-of select="."/>
    </xsl:result-document>
  </xsl:when>
  <xsl:when test="*/@version='2'">
    <xsl:result-document saxon:next-in-chain="stylesheet2.xsl">
       <xsl:copy-of select="."/>
    </xsl:result-document>
  </xsl:when>
</xsl:choose>
</xsl:template>

Michael Kay
http://www.saxonica.com/

Current Thread