This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: additional question: FO subscript Revisited


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]