itsfoobar
itsfoobar

Reputation: 52

XSLT: Concatenate a string n times

I want to concatenate a string for n times and set it as variable using XSLT 2.0. The string to be concatenated is ../, n is available as number in a variable.

I tried:

<xsl:variable name="count-dirs" select="count(tokenize($target-filepath, '/')) - 1"/>
<xsl:variable name="path" select="''"/>
<xsl:for-each select="1 to $count-dirs"><xsl:variable name="path" select="concat($path, '../')"/></xsl:for-each>

Upvotes: 0

Views: 53

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167571

I think you want e.g. <xsl:variable name="path" select="string-join((1 to $count-dirs)!'../', '')"/> in XSLT 3 or <xsl:variable name="path" select="string-join(for $i in 1 to $count-dirs return '../', '')"/> in XSLT 2.

Upvotes: 1

itsfoobar
itsfoobar

Reputation: 52

Life can be so easy...

<xsl:variable name="count-dirs" select="count(tokenize($target-filepath, '/')) - 1"/>
<xsl:variable name="path">
    <xsl:for-each select="1 to $count-dirs">../</xsl:for-each>
</xsl:variable>

Upvotes: 0

Related Questions