<xsl:fallback> 的範例
fallback.xsl 樣式表提供了其他處理 XML 文件的方式。 fallback.xsl 樣式表會嘗試處理具有假設 <xsl:import-table> 項目的 XML 文件。 由於目前的剖析器版本不支援此項目,因此文件轉而使用回溯項目內的指令碼進行處理。
XML 檔 (records.xml)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="fallback.xsl"?>
<records>
<record>
<name>Adam Barr</name>
<address>222 Cherry</address>
<phone>555-797-2355</phone>
</record>
<record>
<name>Jeff Adell</name>
<address>730 Elm</address>
<phone>555-797-5555</phone>
</record>
</records>
XSLT 檔 (fallback.xsl)
<?xml version="1.0"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://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>
輸出
在這個情況下,目前的剖析器版本不支援使用 <xsl:import-table> 項目建立資料表,因此會使用 <xsl:fallback> 項目建立下列資料表:
Adam Barr |
222 Cherry |
555-797-2355 |
Jeff Adell |
730 Elm |
555-797-5555 |