spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / authoring / languages / xml / insidexslt / chap2 / 6 To page 1To page 2To page 3current pageTo page 5
[previous] [next]

Inside XSLT

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

The <xsl:import> Element

Like <xsl:include>, <xsl:import> enables you to insert a stylesheet or stylesheet fragment in another stylesheet. And like <xsl:include>, <xsl:import> has only one attribute:

Also like <xsl:include>, <xsl:import> is empty and has no content. So what's the difference between <xsl:include> and <xsl:import>? The difference lies in import precedence.

Import precedence gives the XSLT processor a way to settle any conflicts that may arise when, for example, two rules match the same node. The precedence of an imported stylesheet or stylesheet fragment is lower than the precedence of the stylesheet that's importing it. And if you import several stylesheets or stylesheet fragments, the first one has lower precedence than the one imported next, which has lower precedence than the one imported after it, and so on.

Otherwise, though, importing a stylesheet or stylesheet fragment looks much like including it, although you use <xsl:import> rather than <xsl:include>:

Listing 2.11: Importing a Stylesheet

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <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">
          <TD>Name</TD>
          <TD>Mass</TD>
          <TD>Radius</TD>
          <TD>Day</TD>
          <xsl:apply-templates/>
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>

</xsl:stylesheet>

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, I'll modify the <xsl:import> example we just saw. In this case, I'll add another column to the HTML table this example produces, labeled DATA, and I'll 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. I'll 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="http://www.w3.org/1999/XSL/Transform">

  <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>

Here's what the new version of rules.xsl looks like:

Listing 2.13: New Version of rules.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <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. I've used one template to build on another, which is the closest you'll get in XSLT to object-oriented inheritance.

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>. You'll get all the details on parameters and <xsl:with-param> in Chapter 9.

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

home / authoring / languages / xml / insidexslt / chap2 / 6 To page 1To page 2To page 3current pageTo page 5
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

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


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