I need to iterate over the characters in a string to build an XML structure.
Currently, I am doing this :
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vText" select="'Some Text'"/>
<xsl:template match="/">
<xsl:for-each select="string-to-codepoints($vText)">
<para><xsl:sequence select="codepoints-to-string(.)"/></para>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
For those of you, learning XSLT 2.0 /XPath 2.0, do note:
The use of the standard XPath 2.0 functions string-to-codepoints() and codepoints-to-string().
In XSLT 2.0 the value of the select
attribute of <xsl:for-each>
may be a sequence of any items, not only nodes.
If the string length is not huge, you can use a recursively called template to achieve this, passing the index of the character to be processed as parameter into the template.
Like so:
<xsl:template name="verticalize">
<xsl:param name="text">Some text</xsl:param>
<xsl:param name="index" select="1" />
<xsl:if test="string-length($text) >= $index">
<xsl:element name="para">
<xsl:value-of select="substring($text, $index, 1)"/>
</xsl:element>
<xsl:call-template name="verticalize">
<xsl:with-param name="text" select="$text" />
<xsl:with-param name="index" select="$index+1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
If the string is longer than that, you can use a similar approach but with a divide-and-conquer algorithm, so that you have a maximum recursion depth of log2(string-length), like so:
<xsl:template name="verticalize">
<xsl:param name="text">Some text</xsl:param>
<xsl:param name="left" select="1" />
<xsl:param name="right" select="string-length($text)" />
<xsl:choose>
<xsl:when test="$left = $right">
<xsl:element name="para">
<xsl:value-of select="substring($text, $left, 1)"/>
</xsl:element>
</xsl:when>
<xsl:when test="$left < $right">
<xsl:variable name="middle" select="floor(($left+$right) div 2)" />
<xsl:call-template name="verticalize">
<xsl:with-param name="text" select="$text" />
<xsl:with-param name="left" select="$left" />
<xsl:with-param name="right" select="$middle" />
</xsl:call-template>
<xsl:call-template name="verticalize">
<xsl:with-param name="text" select="$text" />
<xsl:with-param name="left" select="$middle+1" />
<xsl:with-param name="right" select="$right" />
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
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>
<xsl:template name="letters">
<xsl:param name="text" select="'Some text'" />
<xsl:if test="$text != ''">
<xsl:variable name="letter" select="substring($text, 1, 1)" />
<para><xsl:value-of select="$letter" /></para>
<xsl:call-template name="letters">
<xsl:with-param name="text" select="substring-after($text, $letter)" />
</xsl:call-template>
</xsl:if>
</xsl:template>