xsl include vs import
I've recently been asked by a friend to explain the difference between include and import and how it would affect the templates being matched in these files. The answer is very simple, but you also must understand that there are other ways to foce which template will be called in the case that were are identical matchings.
First of all, let's explain "order of precedence". Basically, it gives the xslt transform engine the order in which templates will be prioritized. If two templates in the same document match the same path, then we cannot guarantee which will take precendence unless we specificy the priority attribute on the template. (The value of this attribute is an integer where the lower value has a higher precendence).
When we are importing an xsl file, using xsl:import, this is different. The file being imported always has a lower precedence than the current file. This means that if you have a template matching //Donkey and include a file called specialdonkey.xsl which contains a template //Donkey then the one in the current file will always be called over the one in the specialdonkey.xsl file.
When you include a file using xsl:include, you have the exact same order of precedence as the current file. Therefore, it is just like if the included file was copy/pasted into the current file. As we stated earlier, templates in the same file, that match the same xpath, who do not have a priority attribute, cannot guarantee which will be called. Therefore, if you plan on including an xsl file, make sure that it does not have conflicting templates and if so, either use the priority attribute, or take a look at our mode trick below.
/********Brief Description **********/
There is a common problem in XSLT where you have two XML of the same xPath (whether it be absolute or relative) that end up taking the same template. In many scenarios you do not have the ability to modify the XML so that it suits your XSLT and you must therefore turn to the mode trick to create seperate templates.
For the following example, say you had the following XML.
<FirstCartProduct> <Product id="123" name="Apples" weight="2.4jg" price="$5.99"/> </FirstCartProduct> <CartReferences> <Product ref="98342kjsd" id="123"/> </CartReferences> </Shop> |
Now as you can see here, you could enter the FirstCartProduct template and want to do an apply-templates on your Product so that you display the products but then you would enter CartReferences, do any apply templates and end up with a buggy output since these Products do not have the same fields.
What we can do to resolve this issue is to give the templates a mode. By supplying the attribute mode with a unique identifier as a value, you can have multiple templates that match the same xPath but are applied from different sources.
For example, in the FirstCartProduct template, we could do an and then we would have a template that can only be applied from there (which means that the one in CartReference would no longer be applied) by doing this:
<xsl:template match="Product" mode="DisplayProduct"> Name: <xsl:value-of select="@name"> </xsl:template> |
No comments:
Post a Comment