Home > Mobile >  XSLT optimization: multiple templates or xsl:choose?
XSLT optimization: multiple templates or xsl:choose?

Time:02-21

In XSLT, I have a modal template. Applied to certain elements (let's say A|B|C) it should return true. Applied to some other elements (let's say D|E|F) it should return false. For all other elements, it should print an error message.

I could do this with one template, <xsl:template match="*" mode="mymode"> where within, there is an xsl:choose to direct to the desired result.

Or I could do this with three templates, <xsl:template match="A|B|C" mode="mymode">, <xsl:template match="D|E|F" mode="mymode">, <xsl:template match="*" mode="mymode">.

Is there any reason to prefer one approach over the other? For example, is it more efficient to use multiple templates and avoid the xsl:choose? Or vice versa?

Is the answer different if there are more than 3 outcomes? If there are only 2 outcomes?

CodePudding user response:

I can only answer for one processor, namely Saxon. It's an area where different processors are likely to be very different.

Saxon will try to optimize both large collections of template rules, and xsl:choose with large numbers of branches, but the optimisations it is capable of performing are different in the two cases. So if you have something with a 200-way choice, and performance matters to you, then it's worth doing some experiments to see. But with a 3-way choice it's very unlikely the difference will be measurable.

Don't even think about using performance as the criterion for choosing one coding style over another unless you have good reason to believe that it will make a difference to your bottom line. This one won't.

CodePudding user response:

I don't know which method is more efficient. I like the multiple template approach. It seems more declarative which is something I appreciate about xslt.

CodePudding user response:

I would certainly prefer using xsl:template match="A | B | C" plus xsl:template match="D | E | F" plus xsl:mode on-no-match="fail" to having to having a single template using a nested xsl:choose/xsl:when approach.

The template matching feels more like the "idiomatic" XSLT approach, I find xsl:choose/xsl:when to be cumbersome/verbose.

As for performance, I don't think it matters for the simple example and in more complex cases I would certainly think to answer that you would need to measure performance with your particular XSLT processor.

In the past, when discussions/questions about whether to use separate templates or a single shot one with nested xsl:choose/xsl:when the majority of the XSLT community in my experience was in favour of the separate template approach, with the main exception being one developer, I think, who thought that in his experience you can ensure your code covers all cases/nodes you need to handle in that single template while it is hard, in a large, over a long time developed code base, to find all relevant templates that might handle your input node. I think that has a point but I would certainly nevertheless prefer a coding style with separate templates and with some coding guidelines establishing you need to keep all templates of a certain mode together in your code. In the future of XSLT 4 we might be allowed to wrap all templates belonging to a mode in e.g. <xsl:mode on-no-match="fail"><xsl:template match="A | B | C">..</xsl:template><xsl:template match="D | E | F">..</xsl:template></xsl:mode> to ensure the templates are kept in a container xsl:mode element together anyway: https://qt4cg.org/branch/master/xslt-40/Overview.html#declaring-modes

  • Related