Name

<xsl:fallback>

Defines a template that should be used when an XSLT processor finds an instruction in the stylesheet that it can’t process. Fallback processing can be triggered by an extension element that relies on code that can’t be found. It is also invoked when an XSLT 1.0 processor working in forward processing mode encounters an XSLT 2.0 element.

Category

Instruction.

Required Attributes

None.

Optional Attributes

None.

Content

An XSLT template.

Appears in

<xsl:fallback> appears inside a template.

Defined in

[1.0] XSLT section 15, “Fallback.”

[2.0] XSLT section 18, “Extensibility and Fallback.”

Example

Here is a stylesheet that uses <xsl:fallback> to terminate the transformation if an extension element can’t be found:

<?xml version="1.0"?>
<!-- fallback.xsl -->
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:db="xalan://DatabaseExtension"
  extension-element-prefixes="db">

  <xsl:output method="html"/>

  <xsl:template match="/">
    <html>
      <head>
        <title><xsl:value-of select="report/title"/></title>
      </head>
      <body>
        <h1><xsl:value-of select="report/title"/></h1>
        <xsl:for-each select="report/section">
          <h2><xsl:value-of select="title"/></h2>
          <xsl:for-each select="dbaccess">
            <db:accessDatabase>
              <xsl:fallback>
                <p>Sorry, the database library is not available!</p>
              </xsl:fallback> 
            </db:accessDatabase>
          </xsl:for-each>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

We’ll use this stylesheet against this XML document:

<?xml version="1.0"?>
<!-- use-db.xml ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.