假設您有一個高度綜合的任務,在沒有適當的輸入XML的情況下打印1到1.000.000的數字。 當然,由於具有足夠的諷刺意味,堆棧溢出,直接遞歸將失敗。打印數字從一百萬到一百萬
我想出了下面列出的解決方案:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="end" select="number(1000000)"/>
<xsl:template match="/">
<xsl:call-template name="batches"/>
</xsl:template>
<xsl:template name="batches">
<xsl:param name="start" select="number(1)"/>
<xsl:param name="stop" select="$end"/>
<xsl:param name="ololo"/>
<xsl:if test="$start <= ($end)">
<xsl:choose>
<xsl:when test="$stop = 0">
<xsl:value-of select="$start"/>:<xsl:value-of select="$ololo"/>
<xsl:text>
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="batches">
<xsl:with-param name="start" select="$start"/>
<xsl:with-param name="stop" select="floor($stop div 2)"/>
<xsl:with-param name="ololo" select=" 'A' "/>
</xsl:call-template>
<xsl:call-template name="batches">
<xsl:with-param name="start" select="floor($stop div 2) + $start + 1"/>
<xsl:with-param name="stop" select="floor($stop div 2)"/>
<xsl:with-param name="ololo" select=" 'B' "/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
它在和的libxslt MSXML是雙向。但是它會打印一些重複的數字,在效率方面看起來很尷尬。這可以改進嗎?
你爲什麼叫 「批」 模板兩次? – TarasB 2010-12-05 18:14:50
這是一個愚蠢的(?)實施分而治之模式的嘗試。 – Flack 2010-12-05 18:25:08