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

Re: Parsing ???

Subject: Re: Parsing ???
From: Stuart Brown <stuart.brown@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 28 May 2002 15:07:02 +0100
xsl string parsing position
"S. Asif Imam" wrote:

> Hi All
>
> I have a problem regarding parsing a string and using it in XSL Sheet.
>
> For example ..
> <data>A,vc,dfg,aa,dfr,r</data>
>
> Now I want to use the comma seperated fields in my XSLT.
> Like  match field 1 = something
>     out some thing ..
> match field 2 = something
>     out some thing.
>

You can use a recursive template to break down the string:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <xsl:template match="data">
    <xsl:variable name="dataString" select="text()"/>
    <xsl:call-template name="commaSplit">
      <xsl:with-param name="dataString" select="$dataString"/>
      <xsl:with-param name="position" select="1"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="commaSplit">
    <xsl:param name="dataString"/>
    <xsl:param name="position"/>
    <xsl:choose>
      <xsl:when test="contains($dataString,',')">
        <!-- Select the first value to process -->
        <xsl:call-template name="doWhatever">
          <xsl:with-param name="doWith"
select="substring-before($dataString,',')"/>
          <xsl:with-param name="position" select="$position"/>
        </xsl:call-template>
        <!-- Recurse with remainder of string -->
        <xsl:call-template name="commaSplit">
          <xsl:with-param name="dataString"
select="substring-after($dataString,',')"/>
          <xsl:with-param name="position" select="$position + 1"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <!-- This is the last value so we don't recurse -->
        <xsl:call-template name="doWhatever">
          <xsl:with-param name="doWith" select="$dataString"/>
          <xsl:with-param name="position" select="$position"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Process of individual value here -->
  <xsl:template name="doWhatever">
    <xsl:param name="doWith"/>
    <xsl:param name="position"/>
    <outputValueInElementWithABigLongName position="{$position}">
      <xsl:value-of select="$doWith"/>
    </outputValueInElementWithABigLongName>
  </xsl:template>

  <xsl:template match="*">
    <xsl:apply-templates/>
  </xsl:template>

</xsl:stylesheet>

There's plenty of other examples of this kind of this in the archives.

Cheers,

Stuart






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


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.