0

I want to transform a newline (\n) from my XML document to a 'br' tag in HTML via XSL without adding extra tags.

Example:

<item name="Test">
    <title>Test</title>
    <text>
        <p>Hello
        World</p>
    </text>
</item>

After the Hello is the newline -> '\n'.

What i want:

<article>
    <p>Hello<br>World</p>
</article>

What i tried (not working):

<xsl:template match="\n">
    <br>
        <xsl:apply-templates/>
    </br>
</xsl:template>

Is this possible without adding extra tags ?

pdunker
  • 263
  • 2
  • 12

1 Answers1

0

Below code can produce the result as you wanted in XSLT 1.0

<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="/">
  <article>
<xsl:apply-templates select="/*/*/*"/>
   </article>
 </xsl:template>
 <xsl:template match="/*/*/*">
<p>
<xsl:choose>
     <xsl:when test="not(contains(., '&#xA;'))">
       <xsl:copy-of select="."/>
     </xsl:when>
<xsl:otherwise>
       <xsl:value-of select="substring-before(., '&#xA;')"/>
       <br/>
<xsl:value-of select="substring-after(., '&#xA;')"/>
</xsl:otherwise>
</xsl:choose>
</p>
 </xsl:template>
</xsl:stylesheet>
nawazlj
  • 779
  • 8
  • 18
  • You are assuming there will be at most one newline character in the processed text. I see no basis for such assumption. – michael.hor257k Jul 21 '16 at 09:04
  • @michael.hor257k I just did only on the basis of input and not considered as general case. If he needs he should mention in the question. Anyway I will use recursive to process the text. – nawazlj Jul 21 '16 at 09:09