Re: [xsl] Split string

Subject: Re: [xsl] Split string
From: Jarkko.Moilanen@xxxxxx
Date: Sat, 6 Dec 2003 12:00:25 +0200
Lainaus Ricardo Saraiva <rss@xxxxxxxxxxxxxx>:

< Hello,
< 
< 	How do I split a string into several strings by a given token?
< 
< 	The example that follows shows what is supposed to be done:
< 
< The input:
< 
< <ToName>person1;person2</ToName>
< <ToAddress>mail1;mail2</ToAddress>
< 
< Desired output:
< 
< <To>
<   <Contact>
< 	<Name>person1</Name>
<       <Address>mail1</Address>
<   </Contact>
<   <Contact>
< 	<Name>person2</Name>
<       <Address>mail2</Address>
<   </Contact>
< </To>
< 
< Thanks in advance,
< 
< Ricardo Saraiva.

[jarkko@itcm xslt]$ cat split.xml
<?xml version="1.0"?>
<List>
<ToName>person1;person2;person3</ToName>
<ToAddress>mail1;mail2;mail3</ToAddress>
</List>




<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" indent="yes"/>
                                                                              
                                                                            
<xsl:template match="/List">
<To>
        <xsl:call-template name="split">
                <xsl:with-param name="names" select="ToName"/>
                <xsl:with-param name="addresses" select="ToAddress"/>
        </xsl:call-template>
</To>
</xsl:template>
<xsl:template name="split">
        <xsl:param name="names"/>
        <xsl:variable name="first" select='substring-before($names,";")'/>
        <xsl:variable name='rest' select='substring-after($names,";")'/>
        <xsl:param name="addresses"/>
        <xsl:variable name="first_address"
select="substring-before($addresses,';')"/>
        <xsl:variable name="rest_address"
select="substring-after($addresses,';')"/>
 
        <xsl:if test='$first'>
        <xsl:if test="$first_address">
        <Contact>
        <Name>
        <xsl:value-of select='$first'/>
        </Name>
        <address>
        <xsl:value-of select="$first_address"/>
        </address>
        </Contact>
        </xsl:if>
        </xsl:if>
 
        <xsl:if test='$rest'>
        <xsl:if test='$rest_address'>
                <xsl:call-template name='split'>
                        <xsl:with-param name='names' select='$rest'/>
                        <xsl:with-param name='addresses'
select='$rest_address'/>
                </xsl:call-template>
        </xsl:if>
        </xsl:if>
        <xsl:if test='not($rest)'>
        <xsl:if test='not($rest_address)'>
                <Contact>
                <Name>
                <xsl:value-of select='$names'/>
                </Name>
                <address>
                <xsl:value-of select='$addresses'/>
                </address>
                </Contact>
        </xsl:if>
        </xsl:if>
</xsl:template>
</xsl:stylesheet>

[jarkko@itcm xslt]$ Xalan split.xml split.xsl
<?xml version="1.0" encoding="UTF-8"?>
<To>
<Contact>
<Name>person1</Name>
<address>mail1</address>
</Contact>
<Contact>
<Name>person2</Name>
<address>mail2</address>
</Contact>
<Contact>
<Name>person3</Name>
<address>mail3</address>
</Contact>
</To>



***************************************************
* Jarkko Moilanen                                 *
* Project Manager, ITCM (www.itcm.org)            *
* Profound XML technology Expert                  *
* University of Tampere                           *
* Hypermedia Laboratory                           *
***************************************************

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread