开发者

How to convert newline into <br/> with XSLT? [duplicate]

This question already has answers here: Closed 10 years ago.

Pos开发者_StackOverflowsible Duplicate:

Interpreting newlines with XSLT xsl:text?

How to convert newline into <br/> with XSLT?

I have this:

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

I want to have this:

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


This transformation:

<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>

when applied on this XML document:

<t>Line1
Line2
Line3
</t>

produces the wanted, correct result:

<p>Line1<br />Line2<br />Line3<br /></p>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜