<xsl:import>
declaration.<xsl:include>
and <xsl:import>
. Both declarations must be top-level elements in the stylesheet, i.e. children of the <xsl:stylesheet>
element. However, <xsl:import>
elements must always be the first child elements of <xsl:stylesheet>
.<xsl:include>
and <xsl:import>
are similar, but they differ in one area, which is central to the topic of this blog post: When importing a stylesheet with <xsl:import>
, templates defined in the importing stylesheet, has higher precedence than templates defined in the imported stylesheet.<line>
elements in between:<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<doc>
<xsl:call-template name="header"/>
<xsl:call-template name="lines"/>
<xsl:call-template name="footer"/>
</doc>
</xsl:template>
<xsl:template name="header">
<header>
<xsl:text>Header content goes here</xsl:text>
</header>
</xsl:template>
<xsl:template name="lines">
<lines>
<xsl:for-each select="1 to 5">
<xsl:variable name="linenum" select="."/>
<xsl:call-template name="single-line">
<xsl:with-param name="linenum" select="$linenum"/>
<xsl:with-param name="content" select="concat('Line ', $linenum, ' content goes here')"/>
</xsl:call-template>
</xsl:for-each>
</lines>
</xsl:template>
<xsl:template name="single-line">
<xsl:param name="linenum"/>
<xsl:param name="content"/>
<line number="{$linenum}">
<xsl:value-of select="$content"/>
</line>
</xsl:template>
<xsl:template name="footer">
<footer>
<xsl:text>Footer content goes here</xsl:text>
</footer>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<header>Header content goes here</header>
<lines>
<line number="1">Line 1 content goes here</line>
<line number="2">Line 2 content goes here</line>
<line number="3">Line 3 content goes here</line>
<line number="4">Line 4 content goes here</line>
<line number="5">Line 5 content goes here</line>
</lines>
<footer>Footer content goes here</footer>
</doc>
<line>
element to look like this:<line>
<number>x</number>
<content>Line x content goes here</content>
</line>
single-line
template to produce the desired output. That would work, obviously, but it’s not the best solution by far. All updates and fixes made to the original stylesheet would have to be applied to the copy as well. DRY, right?<xsl:import>
declaration to import the stylesheet general.xsl
into a second, smaller stylesheet, and then override the single-line
template. Here’s the complete stylesheet:<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:import href="general.xsl"/>
<xsl:template name="single-line">
<xsl:param name="linenum"/>
<xsl:param name="content"/>
<line>
<number>
<xsl:value-of select="$linenum"/>
</number>
<content>
<xsl:value-of select="$content"/>
</content>
</line>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<header>Header content goes here</header>
<lines>
<line>
<number>1</number>
<content>Line 1 content goes here</content>
</line>
<line>
<number>2</number>
<content>Line 2 content goes here</content>
</line>
<line>
<number>3</number>
<content>Line 3 content goes here</content>
</line>
<line>
<number>4</number>
<content>Line 4 content goes here</content>
</line>
<line>
<number>5</number>
<content>Line 5 content goes here</content>
</line>
</lines>
<footer>Footer content goes here</footer>
</doc>
<line>
element has been updated according to the new requirement, but the rest of the output remains unchanged. Mission accomplished, without duplicated code!You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
37 | |
10 | |
7 | |
5 | |
5 | |
5 | |
4 | |
4 | |
3 | |
3 |