xsl-list
[Top] [All Lists]

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

2015-02-09 14:42:38
On Mon, Feb 09, 2015 at 08:21:16PM -0000, dvint(_at_)dvint(_dot_)com 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
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>