How to show character n times in XSLT?

I have a template with a parameter. How can I insert a tab character n times?

n is the parameter value.

+7
source share
6 answers

Just call it recursively; display the tab, then call the same pattern again when n-1 passed, if n> 1.

<xsl:template name="repeat"> <xsl:param name="output" /> <xsl:param name="count" /> <xsl:if test="$count &gt; 0"> <xsl:value-of select="$output" /> <xsl:call-template name="repeat"> <xsl:with-param name="output" select="$output" /> <xsl:with-param name="count" select="$count - 1" /> </xsl:call-template> </xsl:if> </xsl:template> 

As already noted, this example will actually output at least one. In my experience, when output is a space, it is usually necessary. You can adapt the principle of the recursive template as you see fit.

+11
source

In XSLT 2.0:

 <xsl:for-each select="1 to $count">&#x9;</xsl:for-each> 

(Unfortunately, I suspect that if you are using XSLT 2.0, you will not need to ask a question).

Another method often used with XSLT 1.0 is hacking:

 <xsl:for-each select="//*[position() &lt;= $count]">&#x9;</xsl:for-each> 

which works under the condition that the number of elements in the source document is greater than the number of tabs you want to display.

+8
source

Globally define a fairly long array of tabs:

 <xsl:variable name="TABS" select="'&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;'" /> 

Then use like this:

 <xsl:value-of select="fn:substring($TABS, 1, fn:number($COUNT))" /> 
+1
source

It seems the easiest and most flexible for me.

For XSLT 1.0 (or possibly 1.1).

 <xsl:variable name="count">10</xsl:variable> <xsl:variable name="repeat"><xsl:text>&#9;</xsl:text></xsl:variable> <xsl:sequence select="string-join((for $i in 1 to $count return $repeat),'')"/> 

Of course, the count variable is for assigning your parameter n .

I used the repeat variable to hold the tab character, but you can just replace the $ repeat character with the single quote tab character in the sequence element. Note: This variable can be longer than 1, which creates a whole bunch of possibilities.

It does not use recursion, so it will not work in the recursion limit.

I donโ€™t know what maximum value you can use for counting, but I tested it up to 10,000.

+1
source

(XSLT 1.0)

 <xsl:template name="tabs"> <xsl:param name="n"/> <xsl:if test="$n > 0"> <!-- When n = 0, output nothing. --> <xsl:call-template name="tabs"> <!-- Recursive call: call same template... --> <xsl:with-param name="n" select="$n - 1"/> <!-- ... for writing n - 1 tabs. --> </xsl:call-template> <xsl:text>&#x9;</xsl:text> <!-- Add one tab character. --> </xsl:if> </xsl:template> 

Usage example:

 <xsl:call-template name="tabs"> <xsl:with-param name="n" select="3"/> </xsl:call-template> 
+1
source

I discovered a library licensed by LGPL, called functx for this, since I was sure that someone should have done it ... This is the XSLT library of the standard library that contains the repeat-string function. From the docs:

The functx: repeat-string function returns a string consisting of the given number of copies of $ stringToRepeat merged together.

Where I use it in this code:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:functx="http://www.functx.com"> <xsl:import href="../buildlib/functx-1.0.xsl"/> <xsl:output omit-xml-declaration="yes" /> <xsl:variable name="INDENT" select="' '" /> .... <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*" /> </xsl:copy> </xsl:template> <xsl:template match="data-pusher-properties"> <xsl:for-each select="property"> <xsl:choose> ... <xsl:when test="boolean(@value = '${pusher.notifications.server}')"> <xsl:value-of select="functx:repeat-string($INDENT, @indent)" /> <xsl:text>&quot;</xsl:text> <xsl:value-of select="@name" /> <xsl:text>&quot;: </xsl:text> <xsl:text>&quot;</xsl:text> <xsl:value-of select="$pusher.notifications.email.server" /> <xsl:text>&quot;\&#xA;</xsl:text> </xsl:when> ... </xsl:choose> </xsl:for-each> </xsl:template> </xsl:stylesheet> 

So, to print a tab character n times, call it like this:

 <xsl:value-of select="functx:repeat-string('&#x9;', n)" /> 

I know this question is old, but I hope it can still help someone.

Documentation for Repeat String Function

0
source

All Articles