Re: [xsl] text output with some significant white space

Subject: Re: [xsl] text output with some significant white space
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 19 Apr 2001 17:58:35 +0100
Hi Steve,

> I tried <xsl:text> to add spaces: <xsl:text> </xsl:text> will put a
> space in output but it isn't easy to visually distinguish one number
> of spaces from another. as needed for indention of say {3 6 9 12
> ...} spaces. It also balloons line out making very hard read.

OK... you could try using the concat() function with strings instead.
Note that the character entity reference &#xA; gives a line break:

<xsl:template match="foo">
   <xsl:value-of select="concat('begin ', name, ' {&#xA;',
                                '   ', body, '&#xA;',
                                '   }')" />

(Here I've maintained the initial lines - it's equivalent to:

<xsl:template match="foo">
   <xsl:value-of select="concat('begin ', name, ' {&#xA;   ',
                                body, '&#xA;   }')" />

Alternatively, you could set a variable to the requisite number of
spaces, and then use that value:

<xsl:template match="foo">
   <xsl:variable name="indent" select="'   '" />
   <xsl:text />begin <xsl:value-of select="name" />{&#xA;<xsl:text />
   <xsl:value-of select="$indent" /><xsl:value-of select="body" />
   <xsl:value-of select="$indent" />}<xsl:text />

If you made that a parameter, then you could change the level of the
indent as you called templates within it.  Note that I've used the
Allouche method in the above, with empty xsl:text elements delimiting
insignificant whitespace where it abuts significant characters.

Or obviously, a mixture of the two:

<xsl:template match="foo">
   <xsl:variable name="indent" select="'   '" />
   <xsl:value-of select="concat('begin ', name, '{&#xA;',
                                $indent, body, '&#xA;',
                                $indent, '}')" />

If you want to be more explicit about the length of the indent that
you're using, then you could use a variable holding loads of spaces,
and then use substring() to get the number of spaces that you want:

<xsl:variable name="spaces"
              select="'                                         '" />

<xsl:template match="foo">
   <xsl:variable name="indent" select="substring($spaces, 1, 3)" />

The above would give you indents of 3 spaces.

I hope that gives you some ideas,


Jeni Tennison

 XSL-List info and archive:

Current Thread