Re: [xsl] String Manipulation - Distinguishing alphas and numerics in a string

Subject: Re: [xsl] String Manipulation - Distinguishing alphas and numerics in a string
From: Greg Faron <gfaron@xxxxxxxxxxxxxxxxxx>
Date: Wed, 30 Oct 2002 18:38:41 -0700
At 01:13 PM 10/30/2002, you wrote:
Does anyone know how to do this? (the chemistry is not the issue, I realize
this is a bogus combination)


XML source: <para>candybars are made of <chemical>H20ClF3</chemical>.</para>

I don't do FO transformations, but I made this for myself based on what you describe. I'm sure you can modify the <sup> and <sub> elements to be what you need them to be for FO. It turned the above into


candybars are made of H<sub>20</sub>ClF<sub>3</sub>.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:chem="http://www.integretechpub.com/chemistry"; extension-element-prefixes="chem">
<xsl:variable name="subMap" select="'0123456789'"/>
<xsl:variable name="supMap" select="'+-'"/>
<!-- Begin Template: chem:format -->
<xsl:template name="chem:format">
<xsl:param name="molecule" select="''"/>
<xsl:choose>
<xsl:when test="string-length($molecule) = 0"/>
<!-- Test for atom count (subscript) -->
<xsl:when test="contains($subMap, substring($molecule, 1, 1))">
<xsl:variable name="end-index">
<xsl:call-template name="chem:lastIndexOfConsecutiveChars">
<xsl:with-param name="str" select="$molecule"/>
<xsl:with-param name="chars" select="$subMap"/>
</xsl:call-template>
</xsl:variable>
<sub>
<xsl:value-of select="substring($molecule, 1, $end-index)"/>
</sub>
<xsl:call-template name="chem:format">
<xsl:with-param name="molecule" select="substring($molecule, $end-index + 1, string-length($molecule))"/>
</xsl:call-template>
</xsl:when>
<!-- Test for Ion indicator (superscript) -->
<xsl:when test="contains($supMap, substring($molecule, 1, 1))">
<xsl:variable name="end-index">
<xsl:call-template name="chem:lastIndexOfConsecutiveChars">
<xsl:with-param name="str" select="$molecule"/>
<xsl:with-param name="chars" select="$supMap"/>
</xsl:call-template>
</xsl:variable>
<sup>
<xsl:value-of select="substring($molecule, 1, $end-index)"/>
</sup>
<xsl:call-template name="chem:format">
<xsl:with-param name="molecule" select="substring($molecule, $end-index + 1, string-length($molecule))"/>
</xsl:call-template>
</xsl:when>
<!-- Assume atom identifier (normalscript) -->
<xsl:otherwise>
<xsl:value-of select="substring($molecule, 1, 1)"/>
<xsl:call-template name="chem:format">
<xsl:with-param name="molecule" select="substring($molecule, 2, string-length($molecule)- 1)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- End Template: chem:format -->
<!-- Begin Template: chem:lastIndexOfConsecutiveChars -->
<xsl:template name="chem:lastIndexOfConsecutiveChars">
<xsl:param name="str" select="''"/>
<xsl:param name="chars" select="''"/>
<xsl:choose>
<xsl:when test="string-length($chars) = 0 or string-length($str) = 0">
<xsl:value-of select="0"/>
</xsl:when>
<xsl:when test="contains($chars, substring($str, 1, 1))">
<xsl:variable name="result">
<xsl:call-template name="chem:lastIndexOfConsecutiveChars">
<xsl:with-param name="str" select="substring($str, 2)"/>
<xsl:with-param name="chars" select="$chars"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$result + 1"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="0"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- End Template: chem:lastIndexOfConsecutiveChars -->


  <!-- Match all 'chemical' elements -->
  <xsl:template match="chemical">
    <xsl:call-template name="chem:format">
      <xsl:with-param name="molecule" select="."/>
    </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>



Greg Faron
Integre Technical Publishing Co.



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


Current Thread