xsl-list
[Top] [All Lists]

Re: Splitting string

2004-08-02 22:15:13
Hi Rui,
  Please try this XSL. It uses a recursive template.

<?xml version="1.0"?> 
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
 
<xsl:output method="text" /> 
 
<xsl:template match="/root"> 
   <xsl:call-template name="tokenise">
     <xsl:with-param name="str" select="string" />
     <xsl:with-param name="delim" select="'/'" />
     <xsl:with-param name="prev" select="''" />
   </xsl:call-template>
</xsl:template>
 
<xsl:template name="tokenise">
   <xsl:param name="str" />
   <xsl:param name="delim" />
   <xsl:param name="prev" />
   
   <xsl:choose>
     <xsl:when
test="substring-after(substring-after($str,$delim),$delim)
!= ''">
       <xsl:value-of select="$prev" /><xsl:value-of
select="$delim" /><xsl:value-of
select="substring-before(substring-after($str,$delim),$delim)"
/>
       <xsl:text>&#xA;</xsl:text>
       <xsl:call-template name="tokenise">
         <xsl:with-param name="str"
select="concat($delim,substring-after(substring-after($str,$delim),$delim))"
/>
         <xsl:with-param name="delim" select="$delim"
/>     
         <xsl:with-param name="prev"
select="concat($prev,$delim,substring-before(substring-after($str,$delim),$delim))"
/>
       </xsl:call-template>
     </xsl:when>
     <xsl:when
test="substring-after(substring-after($str,$delim),$delim)
= ''">
       <xsl:value-of select="$prev" /><xsl:value-of
select="$str" />
     </xsl:when>
   </xsl:choose>  
</xsl:template>
  
</xsl:stylesheet>

for e.g. when it is applied to XML
<?xml version="1.0"?>
<root>
  <string>/this/is/a/test</string>
</root>

it produces output -
/this
/this/is
/this/is/a
/this/is/a/test

Regards,
Mukul

--- "Rui Alberto L. Gonçalves"
<rui-l-goncalves(_at_)ptinovacao(_dot_)pt> wrote:

Hi all, 
I have a string like:
/this/is/a/test

and I need to create a template that will output:
/this
/this/is
/this/is/a
/this/is/a/test

I think this is not so simple as it looks at first
glance.
Does anyone have an idea how to solve this problem?
Thanks for any help.

Rui
-- 
Rui Alberto L. Gonçalves
<rui-l-goncalves(_at_)ptinovacao(_dot_)pt>
PT Inovação



                
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail 


<Prev in Thread] Current Thread [Next in Thread>