Nick C
Nick C

Reputation: 162

XSLT - Replace multiple new line with single new line

I'm looking to replace multiple consecutive new line characters with a single new line character in the result of an XSLT.

Within an xml node I could have the following:

<description>
    A description that goes over multiple lines, because:

        - someone inputted the data in another system
        - and there are gaps between lines

    Another gap above this
</description>

I'd like the text out of this node to appear as so:

A description that goes over multiple lines, because:
    - someone inputted the data in another system
    - and there are gaps between lines   
Another gap above this

Is there a way to do this with an XSLT? Using XSLT 1.0 (libxslt)

Upvotes: 0

Views: 941

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117100

How about:

<xsl:template match="description">
    <xsl:call-template name="normalize-returns">
        <xsl:with-param name="text" select="."/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="normalize-returns">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&#10;&#10;')">
            <!-- recursive call -->
            <xsl:call-template name="normalize-returns">
                <xsl:with-param name="text">
                    <xsl:value-of select="substring-before($text, '&#10;&#10;')"/>
                    <xsl:text>&#10;</xsl:text>
                    <xsl:value-of select="substring-after($text, '&#10;&#10;')"/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Upvotes: 2

Related Questions