Specifies an XSLT style sheet to include.
<xsl:include href = "URI reference"/>
Number of occurrences | Unlimited |
Parent elements | xsl:stylesheet, xsl:transform |
Child elements | (No child elements) |
An XSLT style sheet can include another XSLT style sheet using the <xsl:include> element. The href attribute value is a URI reference identifying the style sheet to be included. The relative URI is resolved with relation to the base URI of the <xsl:include> element.
The <xsl:include> element is only allowed as the child of the <xsl:stylesheet> element.
Inclusions are processed in MSXML as if they occurred at the tree level, Note that this is not the same as the DOM view of the XML tree. The resource located by the href attribute value is parsed as an XML document, and the children of the <xsl:stylesheet> element in this document replace the <xsl:include> element in the including document. The fact that template rules or definitions are included does not affect the way they are processed.
An error occurs if a style sheet directly or indirectly includes itself.
Including a style sheet multiple times can cause errors because of duplicate definitions. Such multiple inclusions are less obvious when they are indirect. For example, if style sheet B includes style sheet A, style sheet C includes style sheet A, and style sheet D includes both style sheet B and style sheet C, then A will be included indirectly by D twice. If all of B, C, and D is used as independent style sheets, the error can be avoided by separating everything in B other than the inclusion of A into a separate style sheet B1 and changing B to contain just inclusions of B1 and A, doing the same for C, and then changing D to include A, B1, C1.
The following sample demonstrates usage of the <xsl:include> element:
The XML file
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="xslinclude.xsl"?> <COLLECTION> <BOOK> <TITLE>Cosmos</TITLE> <AUTHOR>Carl Sagan</AUTHOR> <PUBLISHER>Ballantine Books</PUBLISHER> </BOOK> <BOOK> <TITLE>Catwings</TITLE> <AUTHOR>Ursula K. Le Guin</AUTHOR> <PUBLISHER>Scholastic</PUBLISHER> </BOOK> <BOOK> <TITLE>Home Town</TITLE> <AUTHOR>Tracy Kidder</AUTHOR> <PUBLISHER>Random House</PUBLISHER> </BOOK> </COLLECTION>
xslinclude.xsl
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:space="preserve"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <xsl:for-each select="COLLECTION/BOOK"> <xsl:apply-templates select="TITLE"/> <xsl:apply-templates select="AUTHOR"/> <xsl:apply-templates select="PUBLISHER"/> <BR/> <!-- add this --> </xsl:for-each> </xsl:template> <xsl:include href="xslincludefile.xsl" /> </xsl:stylesheet>
xslincludefile.xsl
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:space="preserve"> <xsl:template match="TITLE"> Title - <xsl:value-of /><BR/> </xsl:template> <xsl:template match="AUTHOR"> Author - <xsl:value-of /><BR/> </xsl:template> <xsl:template match="PUBLISHER"> Publisher - <xsl:value-of /><BR/><!-- removed second <BR/> --> </xsl:template> </xsl:stylesheet>
Output
Title - Cosmos
Author - Carl Sagan
Publisher - Ballantine Books
Title - Catwings
Author - Ursula K. Le Guin
Publisher - Scholastic
Title - Home Town
Author - Tracy Kidder
Publisher - Random House