13.1 Including Stylesheets

XSLT allows you to use and manage stylesheets in a modular way with the include element. This means that you aren't limited to storing all your template rules in a single stylesheet, but that you can spread them around in as many stylesheets as you want. Included stylesheets must be complete stylesheets, however, not just template rules. You can also include third-party stylesheets, such as those from EXSLT (http://www.exslt.org), which provide extended functionality to XSLT, such as dates, times, math, regular expressions, and much more. (You'll learn more about EXSLT in Chapter 15.)

The XML document in Example 13-1, top.xml (in examples/ch13), lists the three states in the U.S. whose populations were estimated to have grown the most between July 1, 2001 and July 2, 2002, according to the U.S. Census Bureau (see http://eire.census.gov/popest/data/states/tables/ST-EST2002-02.php).

Example 13-1. Fast-growing states in the U.S.
<?xml version="1.0"?>     <PopulationChange segment="Top 3">  <State>   <Name>California</Name>   <Population>35116033</Population>   <Rank>1</Rank>   <Increase>515570</Increase>   <PercentChange>1.5</PercentChange>  </State>  <State>   <Name>Texas</Name>   <Population>21779893</Population>   <Rank>2</Rank>   <Increase>408910</Increase>   <PercentChange>1.9</PercentChange>  </State>  <State>   <Name>New York</Name>   <Population>19157532</Population>   <Rank>3</Rank>   <Increase>73182</Increase>   <PercentChange>0.4</PercentChange>  </State> </PopulationChange>

The include.xsl stylesheet, shown in Example 13-2, processes top.xml with its own templates and with some whitespace-generating templates added by means of an include element (note the bold lines).

Example 13-2. A stylesheet that includes another stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:include href="wspace.xsl"/> <xsl:template match="PopulationChange">  <xsl:text>Population Change: July 1, 2001 to July 1, 2002</xsl:text>  <xsl:call-template name="n1"/>  <xsl:text>Source: US Census Bureau</xsl:text>  <xsl:call-template name="n2"/>  <xsl:apply-templates select="State"/> </xsl:template>     <xsl:template match="State">  <xsl:text>State:</xsl:text>  <xsl:call-template name="sp1"/>  <xsl:value-of select="Name"/>  <xsl:call-template name="n1"/>  <xsl:text>Rank:</xsl:text>  <xsl:call-template name="sp1"/>  <xsl:value-of select="Rank"/>  <xsl:call-template name="n2"/> </xsl:template>     </xsl:stylesheet>

The include element is a top-level element with only one required attribute, href, which references a stylesheet that you want to include wspace.xsl, in this case. The value of href is a URI, which can be an absolute reference (as in http://www.example.com/wspace.xsl or file:///C:/learningxslt/examples/ch13/wspace.xsl) or a relative reference (as in wspace.xsl).

The reference to wspace.xsl in the value of href is a relative reference. This means that the XSLT processor will depend on the location of the including stylesheet include.xsl to determine the relative location of wspace.xsl. In other words, the processor establishes a base URI in relation to the location of the main stylesheet, and then uses that base URI (the main document's location in a filesystem or on the Web) to resolve any relative references that may be used.

All the templates that are called in include.xsl (those named sp1, n1, and n2) are stored in wspace.xsl, which is shown in Example 13-3.

Example 13-3. A stylesheet containing whitespace templates for use elsewhere
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template name="sp1">  <xsl:text>&#32;</xsl:text> </xsl:template>     <xsl:template name="sp2">  <xsl:text>&#32;</xsl:text>  <xsl:text>&#32;</xsl:text> </xsl:template>     <xsl:template name="sp3">  <xsl:text>&#32;</xsl:text>  <xsl:text>&#32;</xsl:text>  <xsl:text>&#32;</xsl:text> </xsl:template>     <xsl:template name="n1">  <xsl:text>&#10;</xsl:text> </xsl:template>     <xsl:template name="n2">  <xsl:text>&#10;</xsl:text>  <xsl:text>&#10;</xsl:text> </xsl:template>     <xsl:template name="n3">  <xsl:text>&#10;</xsl:text>  <xsl:text>&#10;</xsl:text>  <xsl:text>&#10;</xsl:text> </xsl:template>     </xsl:stylesheet>

This stylesheet contains only named templates that emit various amounts of whitespace. None of the templates, however, are invoked by matching patterns within wspace.xsl, so it does nothing on its own it needs some help. Admittedly, wspace.xsl is trivial, but it is also useful and amenable to reuse in a variety of other stylesheets.

Apply include.xsl to top.xml:

xalan top.xml include.xsl

and you will get this output:

Population Change: July 1, 2001 to July 1, 2002 Source: US Census Bureau     State: California Rank: 1     State: Texas Rank: 2     State: New York Rank: 3

You can have multiple include elements in a stylesheet, but it's an error for a stylesheet to include itself. (If you'd like, try this yourself and see what happens!) You can include a stylesheet multiple times with no effect other than having duplicate top-level elements.

Conflicts between such duplicates are resolved using the same conflict resolution rules that exist for any single stylesheet where, for example, the last conflicting template match wins (this is an error, but processors are allowed to recover from this error, with or without issuing a warning message). Having more than one template with the same name in its name attribute, however, is an error from which a processor cannot recover.

You can also chain stylesheets together. I'll show you how by chaining these three stylesheets together: chain.xsl (Example 13-4), state.xsl (Example 13-5), and wspace.xsl (Example 13-3).

This use of the term chain here refers to including a series of stylesheets together. Another more common sense of the term chaining has to do with processing multiple stylesheets in succession, as in a pipeline. Only the former sense is implied in this context.


Here's the first of the bunch, chain.xsl.

Example 13-4. A stylesheet containing an include statement, the start of the chain
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:include href="state.xsl"/>     <xsl:template match="PopulationChange">  <xsl:text>Population Change: July 1, 2001 to July 1, 2002</xsl:text>  <xsl:call-template name="n1"/>  <xsl:text>Source: US Census Bureau</xsl:text>  <xsl:call-template name="n2"/>  <xsl:apply-templates select="State"/> </xsl:template>     </xsl:stylesheet>

It's similar to include.xsl, but it doesn't have a template to match State elements. It needs such a template in order to work. That template is in state.xsl, which chain.xsl includes, shown in Example 13-5.

Example 13-5. The stylesheet meant for inclusion to process the state information
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:include href="wspace.xsl"/> <xsl:template match="State">  <xsl:text>State:</xsl:text>  <xsl:call-template name="sp1"/>  <xsl:value-of select="Name"/>  <xsl:call-template name="n1"/>  <xsl:text>Rank:</xsl:text>  <xsl:call-template name="sp1"/>  <xsl:value-of select="Rank"/>  <xsl:call-template name="n2"/> </xsl:template>     </xsl:stylesheet>

The stylesheet state.xsl also includes wspace.xsl. This describes what I mean by chaining: chain.xsl includes state.xsl, which includes wspace.xsl. This makes all the templates available as if they were in one stylesheet instead of three.

As described in the XSLT specification, what happens when you include a stylesheet is that everything in the included stylesheet is taken into the XSLT processor's stylesheet representation (whatever it may use to create such a representation a hash table or whatever), except the stylesheet element. The processor doesn't make a fuss about where a template or some other top-level element came from.

For all intents and purposes, while still honoring template priority (see Chapter 10), all included stylesheets are lumped together into the same pot with the stylesheet that includes them. Therefore, if you process top.xml with chain.xsl, you will get the same result as when you processed top.xml with include.xsl. Importing a stylesheet instead of including it has a different effect, as you will see in the next section.



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net