xsl:fallback

  1. fallback_1-input.xml

  2. <?xml version="1.0"?>
  3. <products>
  4.   <product id="p1" price="3250" stock="4" country="Denmark"/>
  5.   <product id="p2" price="1000" stock="5" country="Germany"/>
  6.   <product id="p3" price="1200" stock="19" country="Germany"/>
  7.   <product id="p4" price="1500" stock="5" country="Australia"/>
  8.   <product id="p5" price="1225" stock="3" country="Japan"/>
  9. </products>
  1. fallback_1-stylesheet.xsl

  2. <?xml version="1.0"?>
  3. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  4.   <xsl:output indent="yes"/>
  5.   <xsl:template match="/">
  6.     <xsl:variable name="product">
  7.       <xsl:perform-sort select="products/product">
  8.         <xsl:sort select="@stock" data-type="number" order="descending"/>
  9.         <xsl:sort select="@country" order="ascending"/>
  10.         <xsl:fallback>
  11.           <xsl:for-each select="products/product">
  12.             <xsl:sort select="@stock" data-type="number" order="descending"/>
  13.             <xsl:sort select="@country" order="ascending"/>
  14.             <product id="{@id}" price="{@price}" stock="{@stock}" country="{@country}"/>
  15.           </xsl:for-each>
  16.         </xsl:fallback>
  17.       </xsl:perform-sort>
  18.     </xsl:variable>
  19.     <products>
  20.           <xsl:copy-of select="$product"/>
  21.     </products>
  22.   </xsl:template>
  23. </xsl:stylesheet>
  1. fallback_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <products>
  4.   <product id="p3" price="1200" stock="19" country="Germany"/>
  5.   <product id="p4" price="1500" stock="5" country="Australia"/>
  6.   <product id="p2" price="1000" stock="5" country="Germany"/>
  7.   <product id="p1" price="3250" stock="4" country="Denmark"/>
  8.   <product id="p5" price="1225" stock="3" country="Japan"/>
  9. </products>

Comment

This XSLT 2.0 stylesheet has been tested with the following XSLT 1.0 processors. In Oxygen: Success in MSXML 3.0, MSXML 4.0, .Net 1.0, .Net 2.0, and in Xalan; errors in Saxon 6.5.5 and in Xsltproc. In XMLSpy: Errors in MSXML 3.0, 4.0 and 6.0! In Stylus Studio: Success in build-in Java processor, and in all Microsoft's processors!

Updated 2009-03-19