Including XSLTs

Contact Us or call 1-877-932-8228
Including XSLTs

Including XSLTs

It is often useful to reuse a template created in a separate XSLT document. This is easily done with xsl:include, which effectively copies the contents of one XSLT into another. The following examples illustrate the use of xsl:include. First is the XSLT to be included (HTML.xsl) followed by the XSLT that includes it.

Code Sample:

MultipleDocs/Demos/HTML.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template name="StartHTML">
		<xsl:param name="Title" select="'Untitled'"/>
		<html>
			<head>
				<title><xsl:value-of select="$Title"/></title>
			</head>
			<body>
				<h1><xsl:value-of select="$Title"/></h1>
				<ul>
					<xsl:apply-templates select="beatles/beatle">
						<xsl:sort select="name/lastname"/>
					</xsl:apply-templates>
				</ul>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>

Code Sample:

MultipleDocs/Demos/BeatlesInclude.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:include href="HTML.xsl"/>
	<xsl:output method="xml" indent="yes"/>
	<xsl:template match="/">
		<xsl:call-template name="StartHTML">
			<xsl:with-param name="Title" select="'Beatles'"/>
		</xsl:call-template>
	</xsl:template>
	<xsl:template match="beatle[not(@real='no')]">
		<li style="color:blue;"><xsl:apply-templates/></li>
	</xsl:template>
	<xsl:template match="beatle">
		<li style="color:red; text-decoration:line-through">
			<xsl:apply-templates/>
		</li>
	</xsl:template>
	<xsl:template match="name">
		<xsl:value-of select="lastname"/>,
		<xsl:value-of select="firstname"/>
	</xsl:template>
</xsl:stylesheet>

These two documents together work exactly as if the first were pasted into the second. To see how it works, transform MultipleDocs/Demos/BeatlesInclude.xml against MultipleDocs/Demos/BeatlesInclude.xsl.

Next