Importing XSLTs

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

Importing XSLTs

Importing one XSLT into another is very similar to including it. The only difference is that, when importing, templates defined in the importing XSLT take precedence over templates in the imported XSLT. In other words, if there are conflicting templates, the template in the imported XSLT is ignored.

The xsl:import element must be the first element after the open xsl:stylesheet tag. Here is an example of a document that imports HTML.xsl.

Code Sample:

MultipleDocs/Demos/BeatlesImport.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:import 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>

In these examples there is no difference between including and importing HTML.xsl. The difference only comes into play when the including/importing XSLT has conflicting templates with the included/imported XSLT. To see how it works, transform MultipleDocs/Demos/BeatlesImport.xml against MultipleDocs/Demos/BeatlesImport.xsl.

Next