Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When the same XML element matches two XSLT templates through different XPaths, which template executes and why?

Tags:

xslt

Consider this XML:

<people>
  <person>
    <firstName>Deane</firstName>
    <lastName>Barker</lastName>
  </person>
</people>

What if two XSLT templates match an element through different XPaths? I know that if the "match" element on two templates is identical (which should never happen, I don't think), the last template will fire.

However, consider this XSL:

<xsl:template match="person/firstName">
    Template #1
</xsl:template>

<xsl:template match="firstName">
    Template #2
</xsl:template>

The "firstName" element will match on either of these templates -- the first one as a child of "person" and the second one standalone.

I have tested this, and Template #1 executes, while Template #2 does not. What is the operative principle behind this? I can think of three things:

  1. Specificity of XPath (highly doubtful)
  2. Location in the XSLT file (also doubtful)
  3. Some pre-emption of Template #2 by Template #1. Something happens during the execution of Template #1 that tells Template #2 not to execute.
like image 813
Deane Avatar asked Oct 07 '09 14:10

Deane


2 Answers

Your first point is actually correct, there is a defined order described in https://www.w3.org/TR/1999/REC-xslt-19991116#conflict. According to the spec person/firstName has a priority of 0 while firstName has a priority of -0.5. You can also specify the priority yourself using the priority attribute on xsl:template.

like image 94
Jörn Horstmann Avatar answered Nov 15 '22 22:11

Jörn Horstmann


I know that if the "match" element on two templates is identical (which should never happen, I don't think)

This can happen but would not be much point doing this and having two matching templates.

From the spec:

It is an error if this leaves more than one matching template rule. 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.

So in other words you may get an error or it will just use the last template in your XSLT depending on how the processor your are using has been written to handle this situation.

like image 41
Chris R Avatar answered Nov 15 '22 22:11

Chris R