Quantcast
Channel: New Lines to HTML Line Breaks Using XSLT - Stack Overflow
Viewing all articles
Browse latest Browse all 3

New Lines to HTML Line Breaks Using XSLT

$
0
0

I have the following XML:

<RichText>
    Text
    Text
    Text
</RichText>

I would like to output the following HTML using XSLT 1.0 (2.0 if I really really have to):

<p>
    Text<br/>
    Text<br/>
    Text
</p>

I've tried using the following XSL which gets close:

<xsl:template match="text()">
  <xsl:param name="text" select="."/>

  <!-- Because we would rely on $text containing a line break when using 
      substring-before($text,'&#10;') and the last line might not have a
      trailing line break, we append one before doing substring-before().  -->
  <xsl:value-of select="substring-before(concat($text,'&#10;'),'&#10;')"/>
  <br/>

  <xsl:if test="contains($text,'&#10;')">
    <xsl:apply-templates select=".">
      <xsl:with-param name="text" select="substring-after($text,'&#10;')"/>
    </xsl:apply-templates>
  </xsl:if>
<xsl:template>

This outputs:

<p><br>                  
    Text<br>                  
    Text<br>                  
    Text<br>
<br></p>

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images