Re: [xsl] looping on a line (XSLT 1.0 solution)

Subject: Re: [xsl] looping on a line (XSLT 1.0 solution)
From: andrew welch <andrew.j.welch@xxxxxxxxx>
Date: Tue, 29 Nov 2005 15:36:17 +0000
On 11/29/05, JBryant@xxxxxxxxx <JBryant@xxxxxxxxx> wrote:
> > could you give an example of a a recursive
> > function looping on substring-before()?
>
> Slow day here, so...

(Contractors have an easy life (no flames pls)...)

I thought I would provide a 2.0 solution

> Given the following XML file (yours with a parent element):
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <d>
>   <data>389,456,789</data>
>   <data>912</data>
>   <data>917,421</data>
>   <data>389,456,789,561,123,754</data>
> </d>
>
> And given the following transform:
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>
>   <xsl:output method="xml" indent="yes"/>
>
>   <xsl:template match="d">
>     <nos>
>       <xsl:apply-templates/>
>     </nos>
>   </xsl:template>
>
>   <xsl:template match="data">
>     <xsl:call-template name="separate-at-comma">
>       <xsl:with-param name="in-string" select="."/>
>     </xsl:call-template>
>   </xsl:template>
>
>   <xsl:template name="separate-at-comma">
>     <xsl:param name="in-string"/>
>     <xsl:choose>
>       <xsl:when test="contains($in-string, ',')">
>         <no><xsl:value-of select="substring-before($in-string,
> ',')"/></no>
>         <xsl:call-template name="separate-at-comma">
>           <xsl:with-param name="in-string"
> select="substring-after($in-string, ',')"/>
>         </xsl:call-template>
>       </xsl:when>
>       <xsl:otherwise>
>         <no><xsl:value-of select="$in-string"/></no>
>       </xsl:otherwise>
>     </xsl:choose>
>   </xsl:template>
>
> </xsl:stylesheet>

In 2.0 you can save your fingers as its much shorter:

<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:template match="/">
	<nos>
		<xsl:for-each select="for $i in //data return tokenize($i, ',')">
			<no><xsl:value-of select="."/></no>
		</xsl:for-each>
	</nos>
</xsl:template>

</xsl:stylesheet>


> Tested with Saxon 8.6 and Xalan-Java 2.4.1.

Saxon 8.6.1 is out now.

cheers
andrew

Current Thread