Re: [xsl] Removing line breaks without normalize-space()

Subject: Re: [xsl] Removing line breaks without normalize-space()
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Tue, 19 Sep 2006 20:09:01 +0200
In XSLT 1.0 you can do it recursively (not tested myself):

<xsl:template name="removelines" >
<xsl:param name="text" />
<xsl:choose>
<xsl:when test="string-before($text, '&#x0A;')>
<xsl:value-of select="string-before($text, '&#x0A;')" />
<xsl:call-template name="removelines">
<xsl:with-param name="text" select="string-after($text, '&#x0A;') />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>


In XSLT 2.0 you can do:
<xsl:value-of select="replace(paragraph, '\n', '')" />

Or perhaps (not sure this is more complete):
<xsl:value-of select="replace(paragraph, '&#xA;|&#xD;', '')" />

Cheers,
Abel Braaksma
http://abelleba.metacarpus.com


Mark Peters wrote:
Hi,

Is there any way to remove line breaks without deleting the whitespace
in-between elements? For example, suppose you started with the
following XML:

<paragraph>
    <sentence>Some words are <i>italicized</i> and some words are in
<b>bold</b>.</sentence>
    <sentence>Some words are <u>underlined</u>.</sentence>
</paragraph>


I'd like to remove the line breaks, but otherwise retain any existing whitespace between elements, resulting in the following XML:

<paragraph><sentence>Some words are <i>italicized</i> and some words
are in <b>bold</b>.</sentence><sentence>Some words are
<u>underlined</u>.</sentence></paragraph>

I'm trying to avoid sentences whose contents run together when I
process the text.

Thanks!

Mark

Current Thread