Saturday, April 28, 2012

Compatible Stylesheets with xsl:fallback


The following code is an example of how to use the xsl:fallback element to achieve full compatibility between XSLT different versions. Imagine that some future 5.0 version of XSLT implements an element. Processors using earlier versions of XSLT wouldn't understand that element, so you can include an element to enable the processor to continue, using elements it understands from an earlier version of XSLT. 

<xsl:stylesheet version="5.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

...<xsl:for-each select="***XPath expression***" >
   <xsl:condition test="***any condition***">
      XSLT 5.0 - OK - XSLT 5.0
         <xsl:fallback>
            <xsl:if test="***any condition***">
               XSLT 1.0 - OK - XSLT 1.0
            </xsl:if> 
         </xsl:fallback>
   </xsl:condition>              
</xsl:for-each>
...

A processor using XSLT 1.0/2.0 will fail to recognize the <xsl:condition> element, but because it contains an <xsl:fallback> element the older processor will evaluate the <xsl:if> test in the <xsl:fallback> element instead of throwing an error.

No comments:

Post a Comment