Conflict Resolution

Contact Us or call 1-877-932-8228
Conflict Resolution

Conflict Resolution

The difference between xsl:include and xsl:import can be confusing. The first thing to note is that the difference only comes into play if there are conflicting templates. For example, if there is a template called Important in both the including/importing XSLT and the included/imported XSLT, then the XSLT processor has to decide what to do. That decision is handled as follows:

  1. Two Templates with Equal Priority. The specification states that it is an error to have two templates in an XSLT with equal priority. However, it gives the XSLT processor the option of reporting this error or using the last-is-first rule, meaning that the template defined later should be used. In practice, XSLT processors generally use the last-is-first rule rather than report an error.
    • To see an example of how this works, transform MultipleDocs/Demos/BeatlesConflict.xml against MultipleDocs/Demos/BeatlesConflict.xsl. Note that this example does not address includes or imports as the conflicting templates are in the same document.
  2. Two Templates with the Same Name. It is an error to have two templates with the same name in one XSLT. This error will be reported.
    • To see an example of how this works, transform MultipleDocs/Demos/BeatlesConflict-NamedTemplate.xml against MultipleDocs/Demos/BeatlesConflict-NamedTemplate.xsl. Note that this example does not address includes or imports as the conflicting templates are in the same document.
  3. Including a Template with Equal Priority. With xsl:include, the XSLT processor treats the included templates as if they were copied and pasted directly into the including XSLT at the location of the include tag. Any conflicts are resolved with the last-is-first rule, so, in practice, included templates are ignored if they have the same or lower priority as a template in the including XSLT.
    • To see an example of how this works, transform MultipleDocs/Demos/BeatlesIncludeConflict-Apply.xml against MultipleDocs/Demos/BeatlesIncludeConflict-Apply.xsl.
    • To see an example of how this can result in a reported error with named templates, transform MultipleDocs/Demos/BeatlesIncludeConflict-Call.xml against MultipleDocs/Demos/BeatlesIncludeConflict-Call.xsl.
  4. Importing a Template with Equal or Greater Priority. With xsl:import, all matching templates defined in the importing XSLT take precedence over templates in the imported XSLT. In other words, all conflicting templates in the imported XSLT are ignored, regardless of priority.
    • To see an example of how this works with templates with the same priority, transform MultipleDocs/Demos/BeatlesIncludeConflict-Apply.xml against MultipleDocs/Demos/BeatlesIncludeConflict-Apply.xsl. This is the same as with included templates.
    • To see an example how this works when importing a template with greater priority, transform MultipleDocs/Demos/BeatlesPriority-Import.xml against MultipleDocs/Demos/BeatlesImportConflict-Priority.xsl. This is the different with included templates in that the imported template is ignored even though it has a higher priority.
    • To see an example of how this works with named templates, transform MultipleDocs/Demos/BeatlesIncludeConflict-Call.xml against MultipleDocs/Demos/BeatlesIncludeConflict-Call.xsl. Unlike with xsl:include, no error is reported; the imported conflicting template is just ignored.

So, in practice, the only difference between importing and including XSLTs is this:

  • Matching templates in the included XSLT have a lower precedence than matching templates with the same priority in the including XSLT.
  • Matching templates in the imported XSLT have a lower precedence than all matching templates in the importing XSLT, regardless of priority.
Next