Re: [xsl] Processing formatted XML?

Subject: Re: [xsl] Processing formatted XML?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 2 May 2002 10:04:30 +0100
Hi Robert,

> Can anybody give me any points as to how to process formatted, for
> the sake of readability, and generate readable ascii Text?
> Specifically I want to know what the stylesheet should do maintain
> format but at the same time remove leading and trailing spaces.

>From your description, I think that you want to preserve the line
break characters ('&#xA;') and get rid of the space characters (' ')
in the value of the note elements. You can do this with the
translate() function -- translating all the spaces into nothing:

<xsl:template match="note">
    <xsl:value-of select="translate(., ' ', '')" />

If you have tabs in the whitespace, you should include them in the
translate as well:

  <xsl:value-of select="translate(., ' &#x9;', '')" />

If you then want to strip the leading and trailing line breaks -- at
the start and end of the string value of the note element, you can use
the substring() function:

  <xsl:variable name="note" select="translate(., ' &#x9;', '')" />
  <xsl:value-of select="substring($note, 2, string-length($note) - 2)"/>

(Note that this assumes that all your notes have a leading and
trailing newline character -- you might want to check for that first.)
> I have two rules
> <xsl:template match="notes">
> <xsl:call-template name="separated-list">
>         <xsl:with-param name="nodes" select="note"/>
>         <xsl:with-param name="separator">
>         </xsl:with-param>
> </xsl:call-template>
> </xsl:template>

Currently, you're not setting the $separator parameter to anything
(well, only to an empty string) with this code. Whitespace in the
stylesheet is stripped out when the stylesheet is read, unless it's
held within an xsl:text element or you use xml:space="preserve", so
the above call is exactly the same as:

  <xsl:call-template name="separated-list">
    <xsl:with-param name="nodes" select="note" />
    <xsl:with-param name="separator"></xsl:with-param>

If you want to set the separator to a newline character followed by
some spaces, then you should use xsl:text to hold that whitespace --
that way it won't be stripped.

  <xsl:call-template name="separated-list">
    <xsl:with-param name="nodes" select="note" />
    <xsl:with-param name="separator">
      <xsl:text>&#xA;    </xsl:text>



Jeni Tennison

 XSL-List info and archive:

Current Thread