Is it possible to emulate StringBuilder in XSLT?

I am trying to emulate the behavior of StringBuilder in XSL. Is there any way to do this. This looks pretty complicated considering the fact that XSLT is a functional programming language

+4
source share
3 answers

You can get accumulating concat with just a little recursion if you look at node-set (as long as you can build an xpath to find node-set), so that you can add arbitrary bits and pieces to and from the stream it starts to get confused.

Try this for starters (also joins):

<xsl:template match="/"> <xsl:variable name="s"> <xsl:call-template name="stringbuilder"> <xsl:with-param name="data" select="*" /><!-- your path here --> </xsl:call-template> </xsl:variable> <xsl:value-of select="$s" /><!-- now contains a big concat string --> </xsl:template> <xsl:template name="stringbuilder"> <xsl:param name="data"/> <xsl:param name="join" select="''"/> <xsl:for-each select="$data/*"> <xsl:choose> <xsl:when test="not(position()=1)"> <xsl:value-of select="concat($join,child::text())"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="child::text()"/> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template> 

All sorts of extensions may be required for this: perhaps you want to trim, perhaps you also want to tunnel through hierarchies. I'm not sure if there is a bulletproof general solution.

+2
source

Take a look at concat() and string-join() , it's possible that you're after.

+4
source

You can use all available standard XPath 2.0 string functions , such as concat() , substring() , substring-before() , substring-after() , string-join() , ... etc.

However , if you need a very fast string implementation (even faster than the .NET string class), you will probably be interested in the C # finger data structure and extension functions that I provided for the Saxon XSLT processor that wrap the finger-based string.

+1
source

All Articles