Reputation: 162
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
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, ' ')">
<!-- recursive call -->
<xsl:call-template name="normalize-returns">
<xsl:with-param name="text">
<xsl:value-of select="substring-before($text, ' ')"/>
<xsl:text> </xsl:text>
<xsl:value-of select="substring-after($text, ' ')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Upvotes: 2