Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL template precedence

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!

like image 236
Maro Avatar asked Apr 18 '11 22:04

Maro


People also ask

What is priority in XSLT?

The optional priority attribute is a real number that ranges from -9.0 to 0.0 to 9.0 that sets the priority of importance for a template. The higher the number, the higher the priority.

How do xsl templates work?

XSLT is used to resolve the problems by applying template specification so the script of an XSLT has defined rules called templates which in turn has a block of codes to apply a specific logic. To select a particular element in a source file we could use a match attribute along with the template.

What is xsl template match?

XSLT <xsl:template>The match attribute is used to associate the template with an XML element. The match attribute can also be used to define a template for a whole branch of the XML document (i.e. match="/" defines the whole document).

How do I apply a template in XSLT?

The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes. If we add a "select" attribute to the <xsl:apply-templates> element, it will process only the child elements that matches the value of the attribute.


1 Answers

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 
like image 88
Wayne Avatar answered Oct 05 '22 14:10

Wayne