Click to See Complete Forum and Search --> : XSL string to elements


wedge76
12-08-2008, 12:22 PM
Hello

I'd like to transform a string value like this:

<Persons>1478</Persons>

into elements like this:

<Persons>
<Person>1</Person>
<Person>4</Person>
<Person>7</Person>
<Person>8</Person>
</Persons>

Many thanks for helps!

jkmyoung
12-10-2008, 11:37 AM
<xsl:template match="Persons">
<xsl:copy>
<xsl:call-template name="Person"/>
</xsl:copy>
</xsl:template>
<xsl:template name="Person">
<xsl:param name="idx" select="1"/>
<xsl:if test="string-length() &gt;= $idx">
<Person>
<xsl:value-of select="substring(., $idx, 1)"/>
</Person>
<xsl:call-template name="Person">
<xsl:with-param name="idx" select="$idx + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>


May cause a stack overflow if the string you pass in is too large.