【问题标题】:How to convert newline into <br/> with XSLT? [duplicate]如何使用 XSLT 将换行符转换为 <br/>? [复制]
【发布时间】:2011-03-19 14:15:12
【问题描述】:

可能重复:
Interpreting newlines with XSLT xsl:text?

如何使用 XSLT 将换行符转换为 &lt;br/&gt;

我有这个:

<text>
some text with 
new lines
</text>

我想要这个:

<p> some text with <br /> new lines </p>

【问题讨论】:

标签: xml xslt newline


【解决方案1】:

这种转变:

<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, '&#xA;'))">
       <xsl:copy-of select="$pText"/>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="substring-before($pText, '&#xA;')"/>
       <br />
       <xsl:call-template name="insertBreaks">
         <xsl:with-param name="pText" select=
           "substring-after($pText, '&#xA;')"/>
       </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>

【讨论】:

  • 它不起作用。我使用 jquery.xslt.js 进行翻译。你觉得这很重要吗?
  • @liysd:我已经更正了我的答案。立即尝试。
  • @liysd:现在,它可以工作了。 @Dimitre:+1 也是因为我喜欢这种递归模式。
  • @Alejandro:我知道你会喜欢它,否则我建议使用 FXSL 的 str-map() :)
  • @liysd:永远不要写:“它不起作用”。始终解释什么是“它”,什么是“不起作用”。所以“它”之前工作,现在“它”“不工作”?行为的改变可能只是因为今天开始下雨?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-14
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
相关资源
最近更新 更多