Skip to content

XSLT <xsl:fallback>

How to use the XSLT <xsl:fallback> element to provide fallback processing when an XSLT processor doesn't support specific elements.

XSLT <xsl:fallback>

The <xsl:fallback> element specifies an alternate code to run if the XSL processor does not support an XSL element.

Syntax

<xsl:fallback>  
	<!-- Content: template -->  
</xsl:fallback>

Attributes

The <xsl:fallback> element has no attributes.

Example

Consider the following example about a bookstore:

books.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>

<bookstore>
	<book category="children">
		<title lang="en">Harry Potter</title>
		<author>J K. Rowling</author>
		<year>2005</year>
		<price>29.99</price>
	</book>
	<book category="web">
		<title lang="en">XQuery Tips</title>
		<author>Tom Nolan</author>
		<year>2022</year>
		<price>39.99</price>
	</book>
	<book category="web">
		<title lang="en">Learn XML</title>
		<author>Tutorial Reference</author>
		<year>2022</year>
		<price>9.99</price>
	</book>
</bookstore>

This example should run through each "title" element with an invented <xsl:loop> element. If the XSL processor does not support this element (and it does not), it will use the <xsl:for-each> element instead:

books.xslxml
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="bookstore/book">
  <xsl:loop select="title">
    <xsl:fallback>
      <xsl:for-each select="title">
        <xsl:value-of select="."/>
      </xsl:for-each>
    </xsl:fallback>
  </xsl:loop>
</xsl:template>

</xsl:stylesheet>
Navigation