[xsl] Re: Parsing a character string

Subject: [xsl] Re: Parsing a character string
From: Joerg Pietschmann <joerg.pietschmann@xxxxxx>
Date: Thu, 01 Mar 2001 14:00:40 +0100
"Scott Downie" wrote
> I'm flailing about in my attempt to parse a simple string, identify its
> elements, and map those elements to a set of output strings. The input consists
> of seven-character strings that represent the days of the week. One such string
> looks like <<YNYNYYN>>. The first character tells me that, "Yes, the system is
> to send an alert on Sunday," the second tells me, "No, don't send an alert on
> Monday," and so on down through the days of the week. Here is some XML:

There are several more or less elegant solutions. No looping is required
unless you want to be flexible in case the number of days in a week
changes in the near future.

You nearly figured out the most obvious solution:
...
 <xsl:template match="deliverOnDays">
   <td>
     <xsl:choose>
      <xsl:when test="substring($string,1,1)='Y'">
        <xsl:value-of select="'S'" />
      </xsl:when>
       <xsl:otherwise>
        <xsl:value-of select="'_'" />
      </xsl:otherwise>
    </xsl:choose>
    <xsl:choose>
      <xsl:when test="substring($string,2,1)='Y'">
        <xsl:value-of select="'S'" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="'_'" />
      </xsl:otherwise>
    </xsl:choose>
[ five more similar statements omitted ]
  </td>
 </xsl:template>


A somewhat more robust code, if necessary, would be
...
  <xsl:choose>
    <xsl:when test="substring($string,1,1)='Y'">
      <xsl:value-of select="'S'" />
    </xsl:when>
    <xsl:when test="substring($string,1,1)='N'">
      <xsl:value-of select="'_'" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:message>
        <xsl:text>Unexpected code at pos 1, got: </xsl:text>
        <xsl:value-of select=substring($string,1,1)"/>
      </xsl:message>
    </xsl:otherwise>
  </xsl:choose>
... [etcetera]

You could also work with data set lookup:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:data="insert-an-url-here">

<xsl:stylesheet>
  <data:sendflag pos="1" value="Y">S</data:sendflag>
  <data:sendflag pos="1" value="N">_</data:sendflag>
  <data:sendflag pos="2" value="Y">M</data:sendflag>
  <data:sendflag pos="2" value="N">_</data:sendflag>
  <data:sendflag pos="3" value="Y">T</data:sendflag>
  <data:sendflag pos="3" value="N">_</data:sendflag>
  <data:sendflag pos="4" value="Y">W</data:sendflag>
  <data:sendflag pos="4" value="N">_</data:sendflag>
  <data:sendflag pos="5" value="Y">Th</data:sendflag>
  <data:sendflag pos="5" value="N">_</data:sendflag>
  <data:sendflag pos="6" value="Y">F</data:sendflag>
  <data:sendflag pos="6" value="N">_</data:sendflag>
  <data:sendflag pos="7" value="Y">Sa</data:sendflag>
  <data:sendflag pos="7" value="N">_</data:sendflag>

  <xsl:template match="deliverOnDays">
    <td>
      <xsl:value-of select="document('')/xsl:stylesheet/data:sendflag
             [@pos='1' and @value=substring(.,1,1)]"/>
      <xsl:value-of select="document('')/xsl:stylesheet/data:sendflag
             [@pos='2' and @value=substring(.,2,1)]"/>
      <xsl:value-of select="document('')/xsl:stylesheet/data:sendflag
             [@pos='3' and @value=substring(.,3,1)]"/>
      <xsl:value-of select="document('')/xsl:stylesheet/data:sendflag
             [@pos='4' and @value=substring(.,4,1)]"/>
      <xsl:value-of select="document('')/xsl:stylesheet/data:sendflag
             [@pos='5' and @value=substring(.,5,1)]"/>
      <xsl:value-of select="document('')/xsl:stylesheet/data:sendflag
             [@pos='6' and @value=substring(.,6,1)]"/>
      <xsl:value-of select="document('')/xsl:stylesheet/data:sendflag
             [@pos='7' and @value=substring(.,7,1)]"/>
    </td>
  </xsl:template>
...

(Beware: untested!)


There may be a creative way to use a loop here <grin>:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:data="insert-an-url-here">

<xsl:stylesheet>
  <data:sendflag pos="1" value="Y">S</data:sendflag>
   [...more as above...]

  <data:index>1</data:index>
  <data:index>2</data:index>
  <data:index>3</data:index>
  <data:index>4</data:index>
  <data:index>5</data:index>
  <data:index>6</data:index>
  <data:index>7</data:index>

  <xsl:template match="deliverOnDays">
    <xsl:variable name="string" select="."/>
    <td>
      <xsl:for-each select="document('')/xsl:stylesheet/data:index">
        <xsl:value-of select="document('')/xsl:stylesheet/data:sendflag
               [@pos=. and @value=substring($string,.,1)]"/>
      </xsl:for-each>
    </td>
  </xsl:template>
...

(Beware: also untested!)

Regards
J.Pietschmann

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


Current Thread