XML technologies / XSLT / XSLT Cookbook / Strings / Duplicating a string n times
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You need to duplicate a string n times, where n is a parameter. For example, you may need to pad a string with spaces to achieve a particular alignment.
Solution
XSLT 1.0
An elegant recursive solution doubles the input string until the required length is reached, carefully handling cases where $count is odd:
<xsl:template name="dup">
<xsl:param name="input"/>
<xsl:param name="count" select="2"/>
<xsl:choose>
<xsl:when test="not($count) or not($input)"/>
<xsl:when test="$count = 1">
<xsl:value-of select="$input"/>
</xsl:when>
<xsl:otherwise>
<!-- Wenn $count ungerade ist, wird eine zusätzliche Kopie der Eingabe angehängt -->
<xsl:if test="$count mod 2">
<xsl:value-of select="$input"/>
</xsl:if>
<!-- Rekursives Anwenden des Templates nach dem Verdoppeln der Eingabe und dem Halbieren des Zählers -->
<xsl:call-template name="dup">
<xsl:with-param name="input" select="concat($input,$input)"/>
<xsl:with-param name="count" select="floor($count div 2)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>XSLT 2.0
In 2.0, duplication is easy using a for expression. We overload dup to reproduce the default-argument behavior of the XSLT 1.0 implementation:
<xsl:function name="ckbk:dup">
<xsl:param name="input" as="xs:string"/>
<xsl:sequence select="ckbk:dup($input,2)"/>
</xsl:function>
<xsl:function name="ckbk:dup">
<xsl:param name="input" as="xs:string"/>
<xsl:param name="count" as="xs:integer"/>
<xsl:sequence select="string-join(for $i in 1 to $count return $input,'')"/>
</xsl:function>Discussion
XSLT 1.0
The most obvious way to duplicate a string $count times is to concatenate it with itself $count-1 times. This can be done recursively with the following code, but it is expensive for large values of $count and is therefore not recommended:
<xsl:template name="slow-dup">
<xsl:param name="input"/>
<xsl:param name="count" select="1"/>
<xsl:param name="work" select="$input"/>
<xsl:choose>
<xsl:when test="not($count) or not($input)"/>
<xsl:when test="$count=1">
<xsl:value-of select="$work"/><
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="slow-dup">
<xsl:with-param name="input" select="$input"/>
<xsl:with-param name="count" select="$count - 1"/>
<xsl:with-param name="work" select="concat($work,$input)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>The better approach shown above limits recursive calls and concatenations to order log2($count) by repeatedly doubling the input and halving the counter while it exceeds 1. The slow-dup implementation is tricky because it needs an artificial working parameter to track the original input. Recursion may also grow its stack by $count-1 and requires $count-1 calls to concat(). Compare this with dup, which limits stack growth to floor(log2($count)) and requires only ceiling(log2($count)) calls to concat().
Note:
In favor of the slow-dup technique, it can also duplicate structure as well as strings if we replace xsl:value-of with xsl:copy-of. The faster dup has no advantage here, since passing the copies as parameters is expensive.
Another solution, based on but not identical to the EXSLT code str:padding, is:
<xsl:template name="dup">
<xsl:param name="input"/>
<xsl:param name="count" select="1"/>
<xsl:choose>
<xsl:when test="not($count) or not($input)" />
<xsl:otherwise>
<xsl:variable name="string" select="concat($input, $input, $input, $input, $input, $input, $input, $input, $input, $input)"/>
<xsl:choose>
<xsl:when test="string-length($string) >= $count * string-length($input)">
<xsl:value-of select="substring($string, 1, $count * string-length($input))" />
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="dup">
<xsl:with-param name="input" select="$string" />
<xsl:with-param name="count" select="$count div 10" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>This implementation makes ten copies of the input. If that exceeds the requirement, it trims the result to the desired size. Otherwise it recursively applies the template. It is slower because it often performs unnecessary concatenations and uses substring(), which may be slow in some XSLT implementations; see recipe Replacing text for an explanation. It benefits processors without tail-recursion optimization because it greatly reduces recursive calls.
See also
The so-called Piez method can also duplicate a string without recursion. This is discussed in XSLT – Efficient Programming Techniques (PDF). It uses the mechanism for-each (loop ) on any available source of nodes, often the stylesheet itself. Although very effective in practice, I find this inadequate because it assumes enough nodes are available for the required iteration.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de