'Add Line Break With XSLT
I'm trying to add a line break to the XML output with my XSLT.
here is what I'm trying to do.
<Description>
<xsl:value-of select=concat($var1,ADD_LINE_BREAK,$var2,ADD_LINE_BREAK,$var3)"/>
</Description>
* I realize that ADD_LINE_BREAK is not correct XSLT syntax
The xml output would then look something like this:
<Description>
$var1
$var2
$var3
</Description>
Thanks!
UPDATE
It looks like that actually works, but I think I'm figuring out the REAL problem. Quick run down on what I'm doing. Pull XML data from a system -> use XSLT transform to massage the data -> putting xml output into a different system. I think my issue is that the system that I'm putting the data into doesn't understand the line breaks, so I might need a way to figure out how to include HTML line breaks, so that the system can consume.
I've tried this with no luck
<Description>
<xsl:value-of select="$var1"/>
<br></br>
<xsl:value-of select="$var2"/>
<br></br>
<xsl:value-of select="$var3"/>
</Decsription>
Solution 1:[1]
Try something like this:
<Description>
<xsl:value-of select="$var1" /><xsl:text>
</xsl:text>
<xsl:value-of select="$var2" /><xsl:text>
</xsl:text>
<xsl:value-of select="$var3" />
</Description>
This will add a line feed (\n
) character. Maybe you will need to add extra <xsl:text>
</xsl:text>
to get the additional linebreaks in your desired output.
Solution 2:[2]
Use either 

or &10;
for a line feed character.
Use either 
or &13;
for a carriage return character.
Solution 3:[3]
Believe it or not, depending on how fussy your XSLT processor or XML validation is, I've found this embarrassingly simple one works when all else -- like 
or &10;
, etc. -- fails (including a literal like <br>,/br>
:
<xsl:text>
</xsl:text>
One or more lines for your preference.
Solution 4:[4]
I had a similar issue where I had a string field called DESCRIPTION, which had line breaks between paragraphs, but the output kept showing up like this:
This was the first paragraph<br><br>This was the second paragraph.
So I used tokenize to separate everything between the <br><br> tags like this:
<xsl:for-each select="tokenize(DESCRIPTION,'<br><br>')">
<item>
<xsl:value-of select="normalize-space(.)"/><br></br><br></br>
</item>
</xsl:for-each>
And it worked for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mithrandir |
Solution 2 | Daniel Haley |
Solution 3 | Pranamedic |
Solution 4 | Todd K |