This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Parsing ???


"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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]