我希望在XSLT的结果中用单个新行字符替换多个连续的新行字符。
在xml节点中,我可以有以下内容:
<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>
我希望这个节点的文本显示如下:
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
是否有一种方法可以用XSLT做到这一点?使用XSLT 1.0 (libxslt)
如何:
<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>