如何使用XSLT将换行转换为<br/>?

liy*_*ysd 23 xml xslt newline

可能重复:
使用XSLT xsl:text解释换行符?

如何将换行符转换<br/>为XSLT?

我有这个:

<text>
some text with 
new lines
</text>
Run Code Online (Sandbox Code Playgroud)

我想要这个:

<p> some text with <br /> new lines </p>
Run Code Online (Sandbox Code Playgroud)

Dim*_*hev 36

这种转变:

<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>
Run Code Online (Sandbox Code Playgroud)

应用于此XML文档时:

<t>Line1
Line2
Line3
</t>
Run Code Online (Sandbox Code Playgroud)

产生想要的,正确的结果:

<p>Line1<br />Line2<br />Line3<br /></p>
Run Code Online (Sandbox Code Playgroud)

  • @liysd:永远不要写:"它不起作用".总是解释什么是"它"和什么是"不工作".所以"它"以前工作,现在"它""不工作"?行为的变化可能只是因为它今天开始下雨了? (2认同)