[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] normalize-space() except ...


Subject: Re: [xsl] normalize-space() except ...
From: "Graydon graydon@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 9 Feb 2015 20:42:33 -0000

On Mon, Feb 09, 2015 at 08:21:16PM -0000, dvint@xxxxxxxxx scripsit:
[that thing where normalize-space() takes the trailing space off a text
node followed by an inline markup element and no one is happy]
> I'm just wondering if there is a better solution for handling this in XSLT
> than basically writing my own normalize-space function. The real solution
> would be to get the importing tool to handle the text properly and
> normalize on import, but that is not an option.

This is always kinda difficult because of the definition of white space.

XSLT 2.0 lets you do useful things with regular expressions and the
Unicode space character category, \p{Zs}, so you can replace all
sequential white space with one regular space, but of course line feeds
aren't spaces so you have to add that in --

<xsl:template match="text()">
    <xsl:value-of select="replace(.,'[\p{Zs}&#x000A;]+','&#x0020;')"/>
</xsl:template>

which won't strip the trailing space.  It might still qualify as having
to re-write normalize-space().

The XSLT 1.0 solution involves substring() and string-length(); you saw
off the last character, normalize the substring of everything but the
last character, and concat() them back together.  This has the
disadvantage of leaking non-breaking spaces.

-- Graydon


Current Thread
Keywords