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

Re: String Manipulation - Distinguishing alphas and nu

Subject: Re: String Manipulation - Distinguishing alphas and numerics in a string
From: Greg Faron <gfaron@xxxxxxxxxxxxxxxxxx>
Date: Wed, 30 Oct 2002 18:38:41 -0700
xsl variable manipulation
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

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.