Reputation: 10814
I know that, generally, if and choose statements should be avoided in deference to pattern matching however I find myself in situation where I must use one or the other (this is caused by my need for sorting).
At any rate I can either use two <xsl:if />
statements or an <xsl:choose />
statement that has a blank <otherwise />
. I want to know which, if either, is more efficient.
Here is some dummy code:
XML
<?xml version="1.0" encoding="utf-8"?>
<news>
<newsItem id="1">
<title>Title 1</title>
</newsItem>
<newsItem id="2">
<title>Title 2</title>
</newsItem>
<newsItem id="3">
<title></title>
</newsItem>
<newsItem id="4">
<title>Title 4</title>
</newsItem>
<newsItem id="5">
<title>Title 5</title>
</newsItem>
<newsItem id="6">
<title>Title 6</title>
</newsItem>
</news>
XSLT 1
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<ol>
<xsl:apply-templates select="/news/newsItem [string(title)]">
<xsl:sort order="descending" data-type="number" select="@id"/>
</xsl:apply-templates>
</ol>
</xsl:template>
<xsl:template match="newsItem">
<xsl:if test="position() < 5">
<li>
<xsl:value-of select="title"/>
<xsl:if test="position() < 2"> less than two</xsl:if>
<xsl:if test="position() = 3"> equals 3</xsl:if>
</li>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
XSLT 2
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<ol>
<xsl:apply-templates select="/news/newsItem [string(title)]">
<xsl:sort order="descending" data-type="number" select="@id"/>
</xsl:apply-templates>
</ol>
</xsl:template>
<xsl:template match="newsItem">
<xsl:if test="position() < 5">
<li>
<xsl:value-of select="title"/>
<xsl:choose>
<xsl:when test="position() < 2"> less than two</xsl:when>
<xsl:when test="position() = 3"> equals 3</xsl:when>
<xsl:otherwise /> <!-- I know this doesn't need to be here, just included for the sake of readability -->
</xsl:choose>
</li>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Thanks for your help!
Upvotes: 4
Views: 6559
Reputation: 163587
It's very unlikely to make a difference. If it does, the difference will depend entirely on what XSLT processor you are using. The answer to performance questions is always: "measure it!".
Upvotes: 1
Reputation: 449
In this instance it wouldn't actually make much difference. Your second 'when' test is only being executed one less time than your first.
That said, I would use the choose/when/otherwise solution if you are never expecting nor wanting both of these conditions to be true. It wouldn't seem logical to test both conditions knowing only one will be true, but depending on the factors being tested this will be inevitable.
Of course the benefit of the 'choose' is that you can easily set a fallback option, but you are not taking advantage of this. Not that it will make much difference but this 'otherwise' line will be executed whenever the two conditions return false even though it contains nothing.
If you were doing this conditional statement in another language like JavaScript you probably wouldn't think twice about using if/else if/else solution - again, because you only want one condition to ever be true.
I'm afraid the use of 'choose' over 'if' is conditional, it all depends on the data you are processing and what you are testing.
This might be useful to an extent, another dev's view: http://xsltbyexample.blogspot.com/2008/02/when-to-use-xslif-and-xslwhen.html
Upvotes: 3
Reputation: 24319
It probably makes no significant difference, but in the version with the two if
tests, both of the XPath conditions are always executed, whereas in the choose
case, if the first if
matches, the second will never have to be executed.
Stylus Studio, Oxygen, etc. all have profilers which measure this sort of thing, because like most optimization, it's usually best to measure and actually see than to try to do it by feel. And the particular XSLT engine will make a huge difference here.
Upvotes: 1
Reputation: 119846
When I looked into this ages ago, the only guidance I could find was in this KB article:
INFO: Techniques to Improve Performance of XSL Transformations
Reduce the usage of xsl:choose/xsl:when/xsl:otherwise. Performance is effected [sic] when the majority of selections fall through the otherwise clause. Therefore, match with when, and try to avoid using otherwise when you know that a particular value exists.
But I'm guessing it would be dependant on the XSLT processor you're using.
Upvotes: 2