WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 6 (3/5) | WebReference

WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 6 (3/5)

To page 1To page 2current pageTo page 4To page 5
[previous] [next]

Inside XSLT

The <xsl:include> Element

Another way to insert stylesheets inside other documents is to use the <xsl:include> element. This element enables you to include the contents of a file at a particular place in a stylesheet. This element has only one attribute:

This element is empty and has no content.

Here's an example. In this case, I'll put part of the stylesheet in planets.xsl into a new document, rules.xml. Then I can include rules.xml in planets.xsl:

Listing 2.9: Including a Stylesheet

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:include href="rules.xsl"/>
  <xsl:template match="/PLANETS">
    <HTML>
      <HEAD>
        <TITLE>
          The Planets Table
        </TITLE>
      </HEAD>
      <BODY>
        <H1>
          The Planets Table
        </H1>
        <TABLE BORDER="2">
          <TD>Name</TD>
          <TD>Mass</TD>
          <TD>Radius</TD>
          <TD>Day</TD>
          <xsl:apply-templates/>
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>
</xsl:stylesheet>

Here's what rules.xsl looks like. Note that it's a full XSL document with the XML declaration and the <xsl:stylesheet> element:

Listing 2.10: rules.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="PLANET">
    <TR>
      <TD><xsl:value-of select="NAME"/></TD>
      <TD><xsl:value-of select="MASS"/></TD>
      <TD><xsl:value-of select="RADIUS"/></TD>
      <TD><xsl:value-of select="DAY"/></TD>
    </TR>
  </xsl:template>
</xsl:stylesheet>

And that's all it takes. Besides using <xsl:include> to insert stylesheets or stylesheet fragments, you can also use <xsl:import>.

Upcoming in XSLT 2.0
One of the issues that XSLT 2.0 is specifically supposed to address is that included documents are supposed to be able to use their own stylesheets. For example, if you include a document written in the XML language MathML, that included document should be able to use its own stylesheet.


To page 1To page 2current pageTo page 4To page 5
[previous] [next]

Created: October 16, 2001
Revised: October 16, 2001


URL: http://webreference.com/authoring/languages/xml/insidexslt/chap2/6/3.html