10.4 Modes

You already know what happens when more than one template matches an identical pattern a conflict arises that may be overcome by template priority. There is also another workaround for template pattern conflicts: modes. Modes are useful when a stylesheet needs to visit a given node many times with varying results, such as when producing a table of contents or a list of authorities, to name a few examples.

The mode attribute is an optional attribute for both the template and apply-templates elements. If you modify these elements each with a matching mode attribute and value, you can match identical patterns with templates, without generating an error. Following is an example of how it works.

Example 10-12, the document hawaii.xml, lists each of the counties in the state of Hawaii in the U.S., along with the largest city in each county.

Example 10-12. Hawaii counties and their largest cities
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="mode.xsl" type="text/xsl"?>     <us>  <state name="Hawaii">   <county name="Hawaii">    <city >Hilo</city>   </county>   <county name="Honolulu">    <city >Honolulu</city>   </county>   <county name="Kauai">    <city >Kapaa</city>   </county>   <county name="Maui">    <city >Kahului</city>   </county>  </state> </us>

Using an XML stylesheet PI, the document references the stylesheet mode.xsl, shown in Example 10-13, which produces HTML output.

Example 10-13. A stylesheet for processing counties in different modes
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/>     <xsl:template match="us/state">  <html>  <head>  <title>State: <xsl:value-of select="@name"/></title>  <style type="text/css">  h1, h2 {font-family: sans-serif;color: blue}  ul {font-size: 16pt}  </style>  </head>  <body>  <h1>State: <xsl:value-of select="@name"/></h1>  <h2>All Counties</h2>  <ul><xsl:apply-templates select="county" mode="county"/></ul>  <h2>Largest Cities (by County)</h2>  <ul><xsl:apply-templates select="county" mode="city"/></ul>  </body>  </html> </xsl:template>     <xsl:template match="county" mode="county">  <li><xsl:value-of select="@name"/></li> </xsl:template>     <xsl:template match="county" mode="city">  <li>   <xsl:value-of select="city"/> (<xsl:value-of select="@name"/>)  </li> </xsl:template>     </xsl:stylesheet>

There are two templates in this stylesheet that match county elements. Because each of the two is invoked in a different mode, no conflict occurs. In the first template in the stylesheet, there are two instances of the apply-templates element, each matching the county pattern and each with a mode attribute that has a unique value (one has a value of county, the other city). There are no name conflicts in XSLT between the values of match and mode attributes.

Later in the stylesheet, there are two templates that also have mode attributes, matching the values used earlier (county and city). In order to work, the value of mode on a template element must match the value of mode in one or more instances of apply-templates.

The outcome of applying mode.xsl to hawaii.xml in Mozilla is shown in Figure 10-1.

Figure 10-1. Displaying hawaii.xml in Mozilla
figs/lxsl_1001.gif

There are several paragraphs about modes in Section 5.7 of the XSLT specification.



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