[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: looping on a line (XSLT 1.0 solution)

Subject: Re: looping on a line (XSLT 1.0 solution)
From: andrew welch <andrew.j.welch@xxxxxxxxx>
Date: Tue, 29 Nov 2005 15:36:17 +0000
xslt in string
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.