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

Re: additional question: FO subscript Revisited

Subject: Re: additional question: FO subscript Revisited
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 21 Nov 2001 14:05:40 +0000
xsl fo subscript
Hi Tanzila,

> I think the character will always be a subscript.

OK. Then you could create a template that is passed a string to
substitute in and a string of characters, those characters being the
characters that you want to subscript within the string.

<xsl:template name="subscript">
  <xsl:param name="string" select="string()" />
  <xsl:param name="chars" select="'25'" />
  ...
</xsl:template>

Then you can work through the chars by recursion. If there aren't any
characters then you can just return the string:

<xsl:template name="subscript">
  <xsl:param name="string" select="string()" />
  <xsl:param name="chars" select="'25'" />
  <xsl:choose>
    <xsl:when test="$chars">
      ...
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

If there are characters, take the first character first and see if the
string contains that character:

<xsl:template name="subscript">
  <xsl:param name="string" select="string()" />
  <xsl:param name="chars" select="'25'" />
  <xsl:choose>
    <xsl:when test="$chars">
      <xsl:variable name="char" select="substring($chars, 1, 1)" />
      <xsl:choose>
        <xsl:when test="contains($string, $char)">
          ...
        </xsl:when>
        <xsl:otherwise>
          ...
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

If it doesn't contain the character, then you can move on to the next
character - call the template with the $chars parameter being the
substring of the current $chars - everything aside from the first
character:

<xsl:template name="subscript">
  <xsl:param name="string" select="string()" />
  <xsl:param name="chars" select="'25'" />
  <xsl:choose>
    <xsl:when test="$chars">
      <xsl:variable name="char" select="substring($chars, 1, 1)" />
      <xsl:choose>
        <xsl:when test="contains($string, $char)">
          ...
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="subscript">
            <xsl:with-param name="string" select="$string" />
            <xsl:with-param name="chars"
                            select="substring($chars, 2)" />
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

If the string does contain the character, then you need to take the
substring of the string before the character and call the template on
that, with the rest of the chars; then give the fo:inline to make the
character subscript; then call the template on the rest of the string,
with the same set of characters:

<xsl:template name="subscript">
  <xsl:param name="string" select="string()" />
  <xsl:param name="chars" select="'25'" />
  <xsl:choose>
    <xsl:when test="$chars">
      <xsl:variable name="char" select="substring($chars, 1, 1)" />
      <xsl:choose>
        <xsl:when test="contains($string, $char)">
          <xsl:call-template name="subscript">
            <xsl:with-param name="string"
              select="substring-before($string, $char)" />
            <xsl:with-param name="chars" select="$chars" />
          </xsl:call-template>

          <fo:inline baseline-shift="sub" font-size="6px">
            <xsl:value-of select="$char" />
          </fo:inline>

          <xsl:call-template name="subscript">
            <xsl:with-param name="string"
              select="substring-after($string, $char)" />
            <xsl:with-param name="chars"
                            select="substring($chars, 2)" />
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="subscript">
            <xsl:with-param name="string" select="$string" />
            <xsl:with-param name="chars"
                            select="substring($chars, 2)" />
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

This will only work if you want to replace individual characters
rather than longer strings. Again, there's an alternative if you want
to replace strings.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 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.