Template Conflict Resolution

Template Conflict Resolution

Another important aspect of template use is conflict resolution . If two templates match the same node or node set, XSLT relies on the priority of the two templates to determine which template to apply.

Each template has a default priority based on the select attribute value. Generally, the more specific the match or expression (such as "PLANET" versus "*" ) is, the higher its priority. Chapter 4 looks at how the processor determines priorities and how the processor deals with templates that have the same priority.

You can also set the priority of a template with the priority attribute. Heres an example; in this case, a rule created with the element <xsl:template priority="1"/> has lower priority than one created with the <xsl:template priority="2"/> element:

Listing 3.8 Setting Template Priority
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:template match="/PLANETS">          <HTML>              <HEAD>                  <TITLE>                      The Planets Table                  </TITLE>              </HEAD>              <BODY>                  <H1>                      The Planets Table                  </H1>                  <TABLE BORDER="2">                      <TR>                          <TD>Name</TD>                          <TD>Mass</TD>                          <TD>Radius</TD>                          <TD>Day</TD>                      </TR>                      <xsl:apply-templates/>                  </TABLE>              </BODY>          </HTML>      </xsl:template>      <xsl:template match="PLANET">         <TR>            <TD><xsl:value-of select="NAME"/></TD>            <TD><xsl:apply-templates select="MASS"/></TD>            <TD><xsl:apply-templates select="RADIUS"/></TD>            <TD><xsl:apply-templates select="DAY"/></TD>         </TR>     </xsl:template>      <xsl:template match="MASS" priority="2">          <xsl:value-of select="."/>          (<I>Very</I> heavy)      </xsl:template>      <xsl:template match="MASS" priority="1">          <xsl:value-of select="."/>      </xsl:template>      <xsl:template match="RADIUS">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="DAY">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>  </xsl:stylesheet> 

The XSLT processor selects the template with the higher priority, and that template adds the text "(<I>Very</I> heavy)" after each mass measurement. Heres the result, where you can see the template with the higher priority was used:

 <HTML>      <HEAD>          <TITLE>              The Planets Table          </TITLE>      </HEAD>      <BODY>          <H1>              The Planets Table          </H1>          <TABLE BORDER="2">              <TR>                  <TD>Name</TD>                  <TD>Mass</TD>                  <TD>Radius</TD>                  <TD>Day</TD>             </TR>             <TR>                 <TD>Mercury</TD>                 <TD>.0553(<I>Very</I> heavy)</TD>                 <TD>1516 miles</TD>                 <TD>58.65 days</TD>             </TR>             <TR>                 <TD>Venus</TD>                 <TD>.815(<I>Very</I> heavy)</TD>                 <TD>3716 miles</TD>                 <TD>116.75 days</TD>             </TR>             <TR>                 <TD>Earth</TD>                 <TD>1(<I>Very</I> heavy)</TD>                 <TD>2107 miles</TD>                 <TD>1 days</TD>             </TR>          </TABLE>      </BODY>  </HTML> 

Upcoming in XSLT 2.0

The question of template priority is one that XSLT 2.0 is supposed to address. In particular, W3C is considering adding a new element, hypothetically named <xsl: next -match/> , that will enable you to select the second best match to a template.

Heres another useful thing to know about priority: If two templates match the same node and no priority has been assigned these templates, the XSLT processor will choose the template that is a more specific match. For example, a match to "PLANET" is preferred over a match to the generic "*" .

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

Similar book on Amazon

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