|
Summary
How much information should I put in my @match attributes?
This part examines the issues solely around how much information should be used in the match attribute of, for example, the <xsl:template> element. Disregard for a moment, if you will, the actual definition for the use of the match patterns so that we can concentrate solely on the relative performance of their use and misuse.
Taking the example XML shown below, let's create three different stylesheets that perform essentially the same task - apply templates to a given element and catch that application in a template in a match. It is the difference in the content of the match pattern that we are interested in for the moment.
Example A:- <?xml version=1.0?> <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:template match=/> <xsl:apply-templates select=root/level1/level2/level3/level4/level5/data/> </xsl:template> <xsl:template match=root/level1/level2/level3/level4/level5/data> <xsl:value-of select=./> </xsl:template> </xsl:stylesheet>
Example B:- <?xml version=1.0?> <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:template match=/> <xsl:apply-templates select=root/level1/level2/level3/level4/level5/data/> </xsl:template> <xsl:template match=data> <xsl:value-of select=./> </xsl:template> </xsl:stylesheet>
Example C:- <?xml version=1.0?> <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:template match=/> <xsl:apply-templates select=root/level1/level2/level3/level4/level5/data/> </xsl:template> <xsl:template match=//data> <xsl:value-of select=./> </xsl:template> </xsl:stylesheet>
You will see that in the above stylesheets it is only the match attribute that differs. The relative performance results of the three stylesheets are as follows:-
MSXML3:- Example B is 5% faster than Example A Example C is 16% slower than Example B Example C is 12% slower than Example A MSXML4:- Example B is 17% faster than Example A Example B and Example C share the same performance Xalan-C:- Example B is 18% faster than Example A Example C is 10% slower than Example B Example C is 9% slower than Example A
You will notice that for all parsers tested the match with just data is the fastest (with the exception of MSXML4 that seems to have assumed internally that data and //data are functionally equivalent - they both disregard the ancestry of the data element).
Why is this important? Firstly, you must appreciate the important fact that a pattern (the value of a match attribute) is **not** the same as an expression (as might be used in a select attribute) - don't believe me? Try doing <xsl:template match=ancestor::*[1]> in a stylesheet and see what happens! A pattern determines what is to be processed by a template - not what is selected by the template. So in a match pattern we need only specify what the template should process - which allows us to reconsider the actual use of the match itself. In Example A and B I had already decided which elements I was going to apply templates to - by virtue of the select expression of the <xsl:apply-templates>. In both examples A and B the template is being applied - but in Example A I have unneccesarily demanded that the template check the ancestry of <data> elements being matched. In a different circumstance where I had <data> elements with different ancestries and I wanted different processing according to that ancestry then I need to specify in each match the ancestry that should be processed by each template.
A tip you may (or may not) find useful in visualising the difference between patterns and expressions - when you write a match pattern read it back from right to left until you have indentified the nodes you want that template to process... with an expression read from left to right until you have identified the node(s) you wish to process.
The conclusion - don't write match patterns that unneccesarily check the ancestry of the nodes you want that template to process.
Cheers - Marrow http://www.MarrowSoft.com - home of Xselerator (XSLT IDE and debugger) http://www.TopXML.com/Xselerator
| Further additional information | |
Updating comments...
Updating comments...
|