Nic Miloslav     Example 69    KEYWORDS      EXAMPLES      AUTHORS     

Often several templates match selected element in XML source. It must be therefore decided which one to use. The templates are ordered according their priority which can be specified with priority attributte. If the template does not have this attribute, its priority is calculated according to several rules. Stylesheet 1 and Stylesheet 2 differ by priority of their templates. Stylesheet 3 shows the default action in the absence of priority attributes. Template CCC has lower priority than CCC/CCC, as it is less specific. Compare Stylesheet 4 and Stylesheet 5. Template CCC has lower priority than both CCC/CCC or AAA/CCC/CCC, but the latest two have the same priority. In such a case an XSLT processor may signal the error; if it does not signal the error, it must recover by choosing, from amongst the matching template rules that are left, the one that occurs last in the stylesheet. In Stylesheet 6 less specific * has lower priority than CCC. Computed priorities ranges fromt -0.5 to 0.5. XSLT spec gives more details.


     XML     HOME     XSL 1     XSL 2     XSL 3     XSL 4     XSL 5     XSL 6      
<xslTutorial >
<AAA id='a1' pos='start'> 
      <BBB  id='b1'/> 
      <BBB  id='b2'/> 
</AAA> 
<AAA  id='a2'> 
      <BBB  id='b3'/> 
      <BBB  id='b4'/> 
      <CCC  id='c1'> 
           <CCC  id='c2'/> 
      </CCC> 
      <BBB  id='b5'> 
           <CCC  id='c3'/> 
      </BBB> 
</AAA> 
</xslTutorial>

     XSL 1     HOME     XML     HTML 1     OUTPUT 1     
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<xsl:apply-templates select="//CCC"/>
</xsl:template>
<xsl:template match="CCC" priority="3">
<H3 style="color:blue">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H3>
</xsl:template>
<xsl:template match="CCC/CCC" priority="4">
<H2 style="color:red">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H2>
</xsl:template>
</xsl:stylesheet>

     HTML 1     HOME     XML     XSL 1     OUTPUT 1     
<H3 style="color:blue">CCC (id=c1)</H3>
<H2 style="color:red">CCC (id=c2)</H2>
<H3 style="color:blue">CCC (id=c3)</H3>

     OUTPUT 1     HOME     XML     XSL 1     HTML 1     

CCC (id=c1)

CCC (id=c2)

CCC (id=c3)


     XSL 2     HOME     XML     HTML 2     OUTPUT 2     
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<xsl:apply-templates select="//CCC"/>
</xsl:template>
<xsl:template match="CCC" priority="4">
<H3 style="color:blue">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H3>
</xsl:template>
<xsl:template match="CCC/CCC" priority="3">
<H2 style="color:red">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H2>
</xsl:template>
</xsl:stylesheet>

     HTML 2     HOME     XML     XSL 2     OUTPUT 2     
<H3 style="color:blue">CCC (id=c1)</H3>
<H3 style="color:blue">CCC (id=c2)</H3>
<H3 style="color:blue">CCC (id=c3)</H3>

     OUTPUT 2     HOME     XML     XSL 2     HTML 2     

CCC (id=c1)

CCC (id=c2)

CCC (id=c3)


     XSL 3     HOME     XML     HTML 3     OUTPUT 3     
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<xsl:apply-templates select="//CCC"/>
</xsl:template>
<xsl:template match="CCC">
<H3 style="color:blue">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H3>
</xsl:template>
<xsl:template match="CCC/CCC">
<H2 style="color:red">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H2>
</xsl:template>
</xsl:stylesheet>

     HTML 3     HOME     XML     XSL 3     OUTPUT 3     
<H3 style="color:blue">CCC (id=c1)</H3>
<H2 style="color:red">CCC (id=c2)</H2>
<H3 style="color:blue">CCC (id=c3)</H3>

     OUTPUT 3     HOME     XML     XSL 3     HTML 3     

CCC (id=c1)

CCC (id=c2)

CCC (id=c3)


     XSL 4     HOME     XML     HTML 4     OUTPUT 4     
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<xsl:apply-templates select="//CCC"/>
</xsl:template>
<xsl:template match="CCC">
<H3 style="color:blue">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H3>
</xsl:template>
<xsl:template match="CCC/CCC">
<H2 style="color:red">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H2>
</xsl:template>
<xsl:template match="AAA/CCC/CCC">
<H2 style="color:green">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H2>
</xsl:template>
</xsl:stylesheet>

     HTML 4     HOME     XML     XSL 4     OUTPUT 4     
<H3 style="color:blue">CCC (id=c1)</H3>
<H2 style="color:green">CCC (id=c2)</H2>
<H3 style="color:blue">CCC (id=c3)</H3>

     OUTPUT 4     HOME     XML     XSL 4     HTML 4     

CCC (id=c1)

CCC (id=c2)

CCC (id=c3)


     XSL 5     HOME     XML     HTML 5     OUTPUT 5     
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<xsl:apply-templates select="//CCC"/>
</xsl:template>
<xsl:template match="CCC">
<H3 style="color:blue">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H3>
</xsl:template>
<xsl:template match="AAA/CCC/CCC">
<H2 style="color:green">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H2>
</xsl:template>
<xsl:template match="CCC/CCC">
<H2 style="color:red">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H2>
</xsl:template>
</xsl:stylesheet>

     HTML 5     HOME     XML     XSL 5     OUTPUT 5     
<H3 style="color:blue">CCC (id=c1)</H3>
<H2 style="color:red">CCC (id=c2)</H2>
<H3 style="color:blue">CCC (id=c3)</H3>

     OUTPUT 5     HOME     XML     XSL 5     HTML 5     

CCC (id=c1)

CCC (id=c2)

CCC (id=c3)


     XSL 6     HOME     XML     HTML 6     OUTPUT 6     
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<xsl:apply-templates select="//CCC"/>
<xsl:apply-templates select="//AAA"/>
</xsl:template>
<xsl:template match="CCC">
<H3 style="color:blue">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H3>
</xsl:template>
<xsl:template match="*">
<H3 style="color:maroon">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H3>
</xsl:template>
</xsl:stylesheet>

     HTML 6     HOME     XML     XSL 6     OUTPUT 6     
<H3 style="color:blue">CCC (id=c1)</H3>
<H3 style="color:blue">CCC (id=c2)</H3>
<H3 style="color:blue">CCC (id=c3)</H3>
<H3 style="color:maroon">AAA (id=a1)</H3>
<H3 style="color:maroon">AAA (id=a2)</H3>

     OUTPUT 6     HOME     XML     XSL 6     HTML 6     

CCC (id=c1)

CCC (id=c2)

CCC (id=c3)

AAA (id=a1)

AAA (id=a2)