Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Templates - basic usage (10/12) >

Template priorities

It may happen that for a single element in the source document there are several matching templates in the XSLT stylesheet. Attribute priority on the xsl:template element can be explicitly used to distinguish which one should be used based on numeric value of the attribute.

XSLT

      <xsl:stylesheet
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
            <xsl:output  method="xml"
                        indent="yes"
                        omit-xml-declaration="yes"/>

            <xsl:template  match="/">
                  <xsl:apply-templates  select="aaa"/>
            </xsl:template>

            <xsl:template  match="aaa"
                        priority="1000">
                  <xxx>
                        <xsl:apply-templates  select="*"/>
                  </xxx>
            </xsl:template>

            <xsl:template  match="*"
                        priority="1">
                  <p1>
                        <xsl:value-of  select="."/>
                  </p1>
            </xsl:template>

            <xsl:template  match="bbb"
                        priority="22">
                  <p22>
                        <xsl:value-of  select="."/>
                  </p22>
            </xsl:template>

            <xsl:template  match="bbb[2]"
                        priority="333">
                  <p333>
                        <xsl:value-of  select="."/>
                  </p333>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <bbb>B1</bbb>
            <bbb>B2</bbb>
            <ccc>C</ccc>
            <ddd>D</ddd>
      </aaa>
Output

      <xxx>
            <p22>B1</p22>
            <p333>B2</p333>
            <p1>C</p1>
            <p1>D</p1>
      </xxx>


Previous chapter: - - -
Next chapter: Sequences
Previous page: Selection of all child elements or all attributes of an element
Next page: Implicit priorities