The xsl:apply-imports Element

The <xsl: apply-imports > Element

If you import a stylesheet with a template for, say, the <PLANET> element, and then define your own <PLANET> element, the imported version is overridden. How do you access the overridden version? You can use the <xsl:apply- imports> element.

In XSLT 1.0, this element has no attributes, and takes no content. In the XSLT 1.1 working draft, the <xsl:apply-imports> element can handle parameters, so this element may contain zero or more <xsl: with-param > elements (see Chapter 9 for the details on parameters).

As an example, Ill modify the <xsl:import> example we just saw. In this case, Ill add another column to the HTML table this example produces, labeled DATA, and Ill do that by overriding the <PLANET> template in rules.xsl with a new <PLANET> template in planets.xsl. The new template simply adds a new column to the table and then uses the old <PLANET> template for the rest of the data. Ill access the old template with <xsl:apply-imports> :

Listing 2.12 Using <xsl:apply-imports>
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="">      <xsl:import href="rules.xsl"/>      <xsl:template match="/PLANETS">          <HTML>              <HEAD>                  <TITLE>                      The Planets Table                  </TITLE>              </HEAD>              <BODY>                  <H1>                      The Planets Table                  </H1>                  <TABLE BORDER="2">                      <TR>                          <TD>Date</TD>                          <TD>Name</TD>                          <TD>Mass</TD>                          <TD>Radius</TD>                          <TD>Day</TD>                      <xsl:apply-templates/>                      </TR>                  </TABLE>              </BODY>          </HTML>      </xsl:template>      <xsl:template match="PLANET">          <TR>              <TD>4/1/2002</TD>              <xsl:apply-imports/>          </TR>      </xsl:template>  </xsl:stylesheet> 

Heres what the new version of rules.xsl looks like:

Listing 2.13 NewVersion of rules.xsl
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="">      <xsl:template match="PLANET">            <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>     </xsl:template>  </xsl:stylesheet> 

You can see the results in Figure 2.4. Ive used one template to build on another, which is the closest youll get in XSLT to object-oriented inheritance.

Figure 2.4. Using <xsl:apply-imports> .

In the XSLT 1.1 working draft, you can also use stylesheet parameters with <xsl:apply-imports> , which means you can use <xsl:with-param> elements as the content of <xsl:apply-imports> . Youll get all the details on parameters and <xsl:with-param> in Chapter 9.

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

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