Share via


xsl:fallback Element

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

The <xsl:fallback> element is designed to handle situations where the parser cannot handle an XSL element that may be part of a new version, or is otherwise an unrecognized extension. It works by calling template content that can provide a reasonable substitute to the behavior of the new element when encountered.

Syntax

<xsl:fallback>
</xsl:fallback>

Attributes

None.

Element Information

Number of occurrences

Unlimited

Parent elements

Any element where the content is a template.

Child elements

Any element that can occur in a template.

Remarks

When an XSL document is first loaded, the XSLT preparser performs validation on all XSL elements. If the version passed in the <xsl:stylesheet> element is greater than that supported by the parser, then whenever an unfamiliar element is encountered, the parser will execute the <xsl:fallback> children of that element (or simply not perform any action if the fallback has no content). If the element is supported, then the fallback templates are never instantiated. Likewise, if the version given in the style sheet is the same as that supported by the browser, then an error will be called.

Fallbacks are part of the forward-processing mechanism that XSLT uses for handling upgrades. By creating alternative ways of handling a command if an element is not supported, forward-processing guarantees that the code used is relatively robust and insensitive to differences in parser conformity.

In addition to handling versioning differences, the fallback function can also be used to handle elements defined by name space extensions. Because adding functionality to the base specification is typically very difficult, name space extensions let developers define functionality beyond that specified by the XSLT specification. If the name space is not supported (no definition for the name space is provided), or if a name space function or element is not defined, then the fallback mechanism can be used to provide alternate functionality.

To guarantee that the extension is interpreted within the XSLT operation, you need to set the extension-element-prefixes attribute in the <xsl:stylesheet> element so that it contains the name of the prefix to interpret. In addition, declare the name space for that prefix.

Example

The following XML document has the "fallback.xsl" style sheet attached. The fallback.xsl style sheet provides alternate processing for the document.

<?xml version="1.0"?>
<?xml-stylesheet href="fallback.xsl" type="text/xsl"?>
<records>
   <record>
      <name>Rachel Valdez</name>
      <address>222 Cherry</address>
      <phone>425-555-0100</phone>
   </record>
   <record>
      <name>James Van Eaton</name>
      <address>777 Elm</address>
      <phone>425-555-0101</phone>
   </record>
</records>

fallback.xsl

The fallback.xsl style sheet attempts to process the XML document with a hypothetical <xsl:import-table> element. Because this element is not supported in the current version of the parser, the document is alternately processed with the script inside the fallback element.

<xsl:stylesheet version="1.1" xmlns:xsl="https://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
     <HTML>
     <HEAD><TITLE>Output Table</TITLE></HEAD>
     <BODY>
    <xsl:import-table href="blah.html" name="sample">
      <xsl:fallback>
      <p>This version of the parser does not support the creation of a table with the 'xsl:import-table' element, so the following table has been generated using the 'fallback' element.</p>
        <table border='2'>
         <xsl:for-each select='records/record'>
           <tr>
            <td><xsl:value-of select='name'/></td>
            <td><xsl:value-of select='address'/></td>
            <td><xsl:value-of select='phone'/></td>
           </tr>
         </xsl:for-each>
        </table>


      </xsl:fallback>
     </xsl:import-table>
      </BODY>
   </HTML> 
  </xsl:template>
</xsl:stylesheet> 

Output

This version of the parser does not support the creation of a table with the 'xsl:import-table' element, so the following table has been generated using the 'fallback' element.

Rachel Valdez
222 Cherry
425-555-0100 
James Van Eaton
777 Elm
425-555-0101 

See Also

Reference

XSLT Elements
element-available Function
function-available Function
system-property Function