<xsl:import> | |
Allows you to import the templates found in another XSLT stylesheet. Unlike <xsl:include>, all templates imported with <xsl:import> have a lower priority than those in the including stylesheet. Another difference between <xsl:include> and <xsl:import> is that <xsl:include> can appear anywhere in a stylesheet, while <xsl:import> can appear only at the beginning. | |
Category | |
Top-level element |
|
Required Attributes | |
|
|
Optional Attributes | |
None. |
|
Content | |
None. <xsl:import> is an empty element. |
|
Appears in | |
<xsl:import> is a top-level element and can appear only as a child of <xsl:stylesheet>. |
|
Defined in | |
XSLT section 2.6.2, Stylesheet Import. |
|
Example | |
Here is a simple stylesheet that we'll import: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="$newline"/> <xsl:apply-templates select="list/title"/> <xsl:apply-templates select="list/listitem"/> </xsl:template> <xsl:template match="title"> <xsl:value-of select="."/> <xsl:text>: </xsl:text> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> </xsl:template> <xsl:template match="listitem"> <xsl:text>HERE IS LISTITEM NUMBER </xsl:text> <xsl:value-of select="position()"/> <xsl:text>: </xsl:text> <xsl:value-of select="."/> <xsl:value-of select="$newline"/> </xsl:template> </xsl:stylesheet> We'll test both this stylesheet and the one that imports it with this XML document: <?xml version="1.0"?> <list> <title>A few of my favorite albums</title> <listitem>A Love Supreme</listitem> <listitem>Beat Crazy</listitem> <listitem>Here Come the Warm Jets</listitem> <listitem>Kind of Blue</listitem> <listitem>London Calling</listitem> <listitem>Remain in Light</listitem> <listitem>The Joshua Tree</listitem> <listitem>The Indestructible Beat of Soweto</listitem> </list> When we process our XML source document with this stylesheet, here are the results: A few of my favorite albums: HERE IS LISTITEM NUMBER 1: A Love Supreme HERE IS LISTITEM NUMBER 2: Beat Crazy HERE IS LISTITEM NUMBER 3: Here Come the Warm Jets HERE IS LISTITEM NUMBER 4: Kind of Blue HERE IS LISTITEM NUMBER 5: London Calling HERE IS LISTITEM NUMBER 6: Remain in Light HERE IS LISTITEM NUMBER 7: The Joshua Tree HERE IS LISTITEM NUMBER 8: The Indestructible Beat of Soweto Now we'll use <xsl:import> in another stylesheet: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:import href="listitem.xsl"/> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="$newline"/> <xsl:apply-templates select="list/title"/> <xsl:apply-templates select="list/listitem"/> </xsl:template> <xsl:template match="listitem"> <xsl:value-of select="position()"/> <xsl:text>. </xsl:text> <xsl:value-of select="."/> <xsl:value-of select="$newline"/> </xsl:template> </xsl:stylesheet> Here are the results created by our second stylesheet: A few of my favorite albums: 1. A Love Supreme 2. Beat Crazy 3. Here Come the Warm Jets 4. Kind of Blue 5. London Calling 6. Remain in Light 7. The Joshua Tree 8. The Indestructible Beat of Soweto Notice that both stylesheets had a template with match="listitem". The template in the imported stylesheet has a lower priority, so it is not used. Only the imported stylesheet has a template with match="title", so the imported template is used for the <title> element. |