XSL template precedence

17,063

The full resolution process is described in section 5.5 of the XSLT spec.

In general, the following rules apply in order (e.g. a template eliminated from consideration due to lower import precedence is eliminated permanently, regardless of its priority):

  1. Imported templates have lower precedence than templates in the primary stylesheet
  2. Templates with a higher value in their priority attribute have higher precedence
  3. Templates without a priority attribute are assigned a default priority. Templates with more specific patterns take precedence.
  4. It's an error if the previous three steps leave more than one template in consideration, but XSLT processors can recover by defaulting to the last one in the file.

In your specific case both templates have the same priority, so #4 above applies. To demonstrate:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match=
             "vehicle_details[preceding-sibling::vehicle_type = '4x4']/*">
        template1
    </xsl:template>
    <xsl:template match="vehicle_details[descendant::color = 'red']/*">
        template2
    </xsl:template>
</xsl:stylesheet>

Applied to this input (both templates match):

<root>
    <vehicle_type>4x4</vehicle_type>
    <vehicle_details>
        <color>red</color>
    </vehicle_details>
</root>

Output:

template2

But if we swap the order of the templates:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="vehicle_details[descendant::color = 'red']/*">
        template2
    </xsl:template>
    <xsl:template match=
             "vehicle_details[preceding-sibling::vehicle_type = '4x4']/*">
        template1
    </xsl:template>
</xsl:stylesheet>

Then the output is:

template1
Share:
17,063

Related videos on Youtube

Maro
Author by

Maro

Updated on October 14, 2021

Comments

  • Maro
    Maro over 2 years

    I have 2 Templates

    <template match="vehicle_details[preceding-sibling::vehicle_type = '4x4']/*">
        ...
    </xsl:template>
    <xsl:template match="vehicle_details[descendant::color = 'red']/*" >
        ...
    </xsl:template>
    

    My question is: which template will take precedence on transformation. And can someone give me an overview/resources about XSL template precedence?

    Thanks in advance!

  • Dimitre Novatchev
    Dimitre Novatchev about 13 years
    A good explanation must make it absolutely clear that import precedence and priority are two different things and regardless how high priority a template in an imported stylesheet has, its precedence is lower than the precedence of any template in the importing stylesheet.
  • Wayne
    Wayne about 13 years
    @Dimitre - I intended the rules to be read sequentially. Maybe that wasn't clear. I've added an additional bit of explanation.
  • Admin
    Admin about 13 years
    +1 Correct answer. Observation: It's bad practice to rely on error recovery mechanism.
  • Peter Krauss
    Peter Krauss over 10 years
    Important for priority values (!): the maximum "default priority" is 0.5, so you can use 1, 2, etc.
  • Jens
    Jens about 7 years
    As for your 4. above: is it possible to ask xsltproc to be --verbose about such conflicts and the conflict candidates? That would be a helpful debug feature.