XSLT何时比if语句更有效?

4
我知道,一般情况下应避免使用if和choose语句,而应采用模式匹配。然而,在处理排序时,我发现自己必须使用其中之一。无论如何,我可以使用两个语句或一个空的的语句。我想知道哪种方式更有效率。
以下是一些虚拟代码:
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() &lt; 5">
            <li>
                <xsl:value-of select="title"/>

                <xsl:if test="position() &lt; 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() &lt; 5">
            <li>
                <xsl:value-of select="title"/>
                <xsl:choose>
                    <xsl:when test="position() &lt; 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>

感谢您的帮助!
4个回答

3
在这种情况下,实际上并没有太大的区别。您的第二个“when”测试仅比第一个少执行一次。
话虽如此,如果您从未预期或希望这两个条件都为真,则应使用choose/when/otherwise解决方案。测试两个条件似乎不合逻辑,因为只有一个条件为真,但根据所测试的因素,这将是不可避免的。
当然,“choose”的好处在于您可以轻松设置回退选项,但您并没有利用它。尽管它包含了空内容,但只要这两个条件返回false,这个“otherwise”行就会被执行。
如果您在其他语言(如JavaScript)中执行此条件语句,您可能不会考虑两次使用if / else if / else解决方案-同样是因为您只想让一个条件为真。
很抱歉,“choose”而不是“if”的使用是有条件的,这完全取决于您正在处理的数据以及您正在测试的内容。
这可能在某种程度上有用,另一位开发者的观点:http://xsltbyexample.blogspot.com/2008/02/when-to-use-xslif-and-xslwhen.html

2
当我很久以前研究这个问题时,我能找到的唯一指导是在这篇KB文章中:INFO: Techniques to Improve Performance of XSL Transformations。减少使用xsl:choose/xsl:when/xsl:otherwise。当大多数选择都通过otherwise子句时,性能会受到影响。因此,请使用when进行匹配,并尽量避免在您知道特定值存在时使用otherwise。但我猜这取决于您使用的XSLT处理器。

1

可能没有太大的区别,但在带有两个if测试的版本中,两个XPath条件总是被执行,而在choose情况下,如果第一个if匹配,则第二个将永远不需要被执行。

Stylus Studio、Oxygen等都有测量这种事情的分析工具,因为像大多数优化一样,最好的方法通常是通过测量和实际查看来完成,而不是试图凭感觉做到。而特定的XSLT引擎在这里会产生巨大的影响。


1

这很不可能有什么区别。如果有区别,那完全取决于您使用的XSLT处理器。对于性能问题的答案总是:“测量它!”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接