这里是一个更高效的解决方案 O(N) vs. O(N^2),适用于已接受的答案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="text()" name="skipAfterDots">
<xsl:param name="pTotalString" select="."/>
<xsl:param name="pTotalLength" select="string-length(.)"/>
<xsl:param name="pPosition" select="1"/>
<xsl:param name="pLastFound" select="-1"/>
<xsl:choose>
<xsl:when test="$pPosition > $pTotalLength">
<xsl:value-of select="substring($pTotalString, $pLastFound + 1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vIsDot" select=
"substring($pTotalString, $pPosition, 1) = '.'"/>
<xsl:call-template name="skipAfterDots">
<xsl:with-param name="pTotalString" select="$pTotalString"/>
<xsl:with-param name="pTotalLength" select="$pTotalLength"/>
<xsl:with-param name="pLastFound" select=
"$pLastFound * not($vIsDot) + $pPosition * $vIsDot"/>
<xsl:with-param name="pPosition" select="$pPosition+1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
当这个转换应用于以下XML文档时:
<t>M:Namespace.Class.Method(Something a, Something b)</t>
得到了想要的、正确的结果:
Method(Something a, Something b)
解释:
这个解决方案没有包含任何对substring-after()
函数的调用。相反,每一步只与字符串的一个字符进行相等性比较。因为最多只有N个字符,这是O(N) -- 线性复杂度。
相反地,被接受的答案在每一步都调用了substring-after()
函数。在最坏的情况下可能会有N个点,因此这将是O(N^N) -- 二次复杂度。
注意: 我们做出合理的假设,在这两种解决方案中,定位字符串的第k个字符是O(1)的。