我希望在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)
答案 0 :(得分:2)
怎么样:
<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>