An XSLT 1.0 Solution using FXSL
The FXSL library offers a number of generic functions for list processing. Almost all of them have an analog for operating on strings (regarding a string as a list of characters).
Here is an example using the str-foldl
function/template:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dvc-foldl-func="dvc-foldl-func"
exclude-result-prefixes="xsl dvc-foldl-func"
>
<xsl:import href="dvc-str-foldl.xsl"/>
<dvc-foldl-func:dvc-foldl-func/>
<xsl:variable name="vFoldlFun" select="document('')/*/dvc-foldl-func:*[1]"/>
<xsl:output encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:call-template name="dvc-str-foldl">
<xsl:with-param name="pFunc" select="$vFoldlFun"/>
<xsl:with-param name="pStr" select="123456789"/>
<xsl:with-param name="pA0" select="0"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="dvc-foldl-func:*">
<xsl:param name="arg1" select="0"/>
<xsl:param name="arg2" select="0"/>
<xsl:value-of select="$arg1 + $arg2"/>
</xsl:template>
</xsl:stylesheet>
This transformation calculates the sum of the characters in the string passed as the $pStr
parameter and produces the correct result:
45
And using the str-map
template/function we have the following easy and short solution:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:testmap="testmap"
exclude-result-prefixes="xsl testmap"
>
<xsl:import href="str-dvc-map.xsl"/>
<!-- to be applied on any xml source -->
<testmap:testmap/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vTestMap" select="document('')/*/testmap:*[1]"/>
<xsl:call-template name="str-map">
<xsl:with-param name="pFun" select="$vTestMap"/>
<xsl:with-param name="pStr" select="'Some Text'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="split" match="*[namespace-uri() = 'testmap']">
<xsl:param name="arg1"/>
<para><xsl:value-of select="$arg1"/></para>
</xsl:template>
</xsl:stylesheet>
When applied on any XML file (not used), the wanted, correct result is produced:
<para>S</para>
<para>o</para>
<para>m</para>
<para>e</para>
<para> </para>
<para>T</para>
<para>e</para>
<para>x</para>
<para>t</para>