如何将换行符转换为< br/>使用XSLT? [英] How to convert newline into <br/> with XSLT?
本文介绍了如何将换行符转换为< br/>使用XSLT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
用XSLT xsl:text解释换行符吗?
Possible Duplicate:
Interpreting newlines with XSLT xsl:text?
如何使用XSLT将换行符转换为<br/>
?
How to convert newline into <br/>
with XSLT?
我有这个:
<text>
some text with
new lines
</text>
我想要这个:
<p> some text with <br /> new lines </p>
推荐答案
此转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="t">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="text()" name="insertBreaks">
<xsl:param name="pText" select="."/>
<xsl:choose>
<xsl:when test="not(contains($pText, '
'))">
<xsl:copy-of select="$pText"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($pText, '
')"/>
<br />
<xsl:call-template name="insertBreaks">
<xsl:with-param name="pText" select=
"substring-after($pText, '
')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
应用于此XML文档:
<t>Line1
Line2
Line3
</t>
产生所需的正确结果:
<p>Line1<br />Line2<br />Line3<br /></p>
这篇关于如何将换行符转换为< br/>使用XSLT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文