While it is common to have several matching templates for a single source node, many stylesheets does not contain explicit priority assignment. If priority of a template is not given it is automatically calculated based on match attribute.
While there are several rules for assignment of priority and numbers are given for them, it is sufficient to remember that templates with names explicitly given have precedence over "*" and that templates with match containing only name of the element are of lower precedence than templates containing further information about the element to be matched.
If there are two matching templates of the same priority the processor may announce an error and stop processing. The standard also permits the processor to resolve the conflict by using the template which occurs last in the stylesheet. Processors using the second possibility usually issue a warning in this case.
|
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"> <xxx> <xsl:apply-templates select="*"/> </xxx> </xsl:template> <xsl:template match="*"> <p1> <xsl:value-of select="."/> </p1> </xsl:template> <xsl:template match="bbb"> <p22> <xsl:value-of select="."/> </p22> </xsl:template> <xsl:template match="bbb[2]"> <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: | Template priorities |
| Next page: | Applying templates with lower priority - xsl:next-match |