xslt - Convert String to Tree Structure -
i'm new xslt (1.0), anyhow want start difficult ....
given source xml stringvalue "432565" (<line>432565</line>
)
output should xml
<element id="1">4</element> <element id="2">43</element> <element id="3">432</element> <element id="4">4325</element> <element id="5">43256</element> <element id="6">432565</element>
currently i've recursive template
<xsl:template match="line" name="letters"> <xsl:param name="text" select="."/> <xsl:if test="$text != ''"> <xsl:variable name="letter" select="substring($text, 1, 1)"/> <element> <xsl:value-of select="$letter"/> </element> <xsl:call-template name="letters"> <xsl:with-param name="text" select="substring-after($text, $letter)"/> </xsl:call-template> </xsl:if> </xsl:template>
that return
<element>4</element> <element>3</element> <element>2</element> <element>5</element> <element>6</element> <element>5</element>
i feel solution not far away, mentioned, i'm new xslt.
as passing in text parameter, pass in "length" parameter, starts @ 1, , incrementing 1 each call of template. can used determine length of text string output.
try template instead
<xsl:template match="line" name="letters"> <xsl:param name="text" select="."/> <xsl:param name="length" select="1"/> <xsl:if test="$length <= string-length($text)"> <xsl:variable name="letter" select="substring($text, 1, $length)"/> <element id="{$length}"> <xsl:value-of select="$letter"/> </element> <xsl:call-template name="letters"> <xsl:with-param name="text" select="$text"/> <xsl:with-param name="length" select="$length + 1"/> </xsl:call-template> </xsl:if> </xsl:template>
note use of attribute value templates in creating id
attribute too.