Re: Line Numbering

Subject: Re: Line Numbering
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 14 Aug 2000 22:15:49 +0100
Darrin,

>I know that xml is not about formatting, but I need  to transform some xml
>to html that will allow me to put sixty charecters on a line and then break.
>I have not seen any discussion on this nor is there anything in the archives
>that I could find.  Is this not possible with xslt?  I have XSLT
>Programmer's Reference but can not seem to get going in the right direction.

This is best done with a recursive named template to which you pass the
string and that outputs it 60 characters at a time.  The relevant function
is substring(), which enables you to get a substring n characters long.

So, first I'm going to declare a parameter that will hold the number of
characters you want per line, just in case you want to vary that later on,
or for different runs of the same stylesheet:

<xsl:param name="break-at" select="'60'" />

Then the recursive template.  You're going to pass it the parameter that is
the string that you want to break up.  If that string is under 60
characters long, then you just output that, otherwise you need to output
the first 60 characters, then a break ('br' element - you can put an actual
line break in instead if you want), and then call the template on the rest
of the string:

<xsl:template nmae="break-string">
  <xsl:param name="string" />
  <xsl:choose>
    <xsl:when test="string-length($string) &lt;= $break-at">
      <xsl:value-of select="$string" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="substring($string, 1, $break-at)" /><br />
      <xsl:call-template name="break-string">
        <xsl:with-param name="string"
                        select="substring($string, $break-at + 1)" />
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Finally, you need to call this recursive template from somewhere, passing
it the original string.  Let's say that the string is the content of a 'p'
element, then you need something like:

<xsl:template match="p">
  <xsl:call-template name="break-string">
    <xsl:with-param name="string" select="." />
  </xsl:call-template>
</xsl:template>

Note that this doesn't take account of the position of spaces in your
string at all, although it would probably be possible to make it do so in
some rudimentary way.

I hope this helps anyway,

Jeni


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


Current Thread