[xsl] Parsing a character string

Subject: [xsl] Parsing a character string
From: "Scott Downie" <sdownie@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 28 Feb 2001 13:31:05 -0600
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:
-----
<transaction name="listAlerts">
  <alertDefinitionGroup count="3">
    <alertDefinition>
      <clientId>DEMOW00001</clientId>
      <AccountNumber>321</AccountNumber>
      <deliveryMethodGroup count="1">
        <deliveryMethod>
          <deliverOnDays>NNYYNNY</deliverOnDays>
        </deliveryMethod>
      </deliveryMethodGroup>
    </alertDefinition>
    <alertDefinition>
      <clientId>DEMOW00002</clientId>
      <AccountNumber>654</AccountNumber>
      <deliveryMethodGroup count="1">
        <deliveryMethod>
          <deliverOnDays>NYYYYYN</deliverOnDays>
        </deliveryMethod>
      </deliveryMethodGroup>
    </alertDefinition>
    <alertDefinition>
      <clientId>DEMOW00003</clientId>
      <AccountNumber>987</AccountNumber>
      <deliveryMethodGroup count="1">
        <deliveryMethod>
          <deliverOnDays>YYYYYYY</deliverOnDays>
        </deliveryMethod>
      </deliveryMethodGroup>
    </alertDefinition>
  </alertDefinitionGroup>
</transaction>
-----
After substituting SMTWThFSa for the appropriate "Yes" flags and substituting an
underscore (or nbsp) for the appropriate "No" flags, the desired HTML output
should be something like:
-----
<table>
  <tr>
    <td>DEMOW00001</td>
    <td>321</td>
    <td>__TW__Sa<td>
  </tr>
  <tr>
    <td>DEMOW00002</td>
    <td>654</td>
    <td>_MTWThF_<td>
  </tr>
  <tr>
    <td>DEMOW00003</td>
    <td>987</td>
    <td>SMTWThFSa<td>
  </tr>
</table>
-----
I tried to modify the method Jeni suggested in "Escaping an apostrophe for
Javascript" (25 Feb), but I couldn't determine where to add the loop necessary
to slice the "Y"s and "N"s apart one at a time.

Then I considered using a series of if statements like--

<xsl:when test="substring($string,1,1)='N'">
  <xsl:value-of select="'_'" />
</xsl:when>
<xsl:when test="substring($string,1,1)='Y'">
  <xsl:value-of select="'S'" />
</xsl:when>
<xsl:when test="substring($string,2,1)='N'">
  <xsl:value-of select="'_'" />
</xsl:when>
<xsl:when test="substring($string,2,1)='Y'">
  <xsl:value-of select="'M'" />
</xsl:when>
...

--but the looping problem reared its ugly head again. I'm still struggling with
where to loop and where not to loop (in the fine tradition of the "*NEVER* use
for-each" thread).

Now I'm looking at Michael Kay's naming-lines.xsl example ("XSLT," p. 173 ff) in
the xsl:call-template command description, but that doesn't seem to quite fit
either.

I'm sure this question must have been asked before, but I couldn't find anything
as I searched the Mulberry archives. There's so much info out there--as well as
in Mr. Kay's book--but good indices seem to be hard to come by (or at least hard
for a newbie to grok).

Thanks!

Scott


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


Current Thread