The xsl:include Element

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:

  • href (mandatory). The URI of the stylesheet you want to include.

This element is empty and has no content.

Heres an example. In this case, Ill 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="">      <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> 

Heres what rules.xsl looks like. Note that its 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="">      <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 thats 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.

Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
Year: 2005
Pages: 196

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: