Convert line break in XML to space with XSLT

Viewed 63

I have some poorly formatted XML files that may contains line breaks within element. For exemple:

<para> This is a
text with random
breaklines </para>

My issue is that when is use XSLT to convert this to HTML, the words separated by line breaks in the xml are actually jointed.

<p> This is atext with randombreaklines </p>

I wish to be able to insert a Space character where there is line breaks in the XML? Is that possible?

1 Answers

The result you show can be produced using:

<xsl:template match="para"> 
    <p>
        <xsl:value-of select="translate(., '&#10;', ' ')"/>
   </p>
</xsl:template>

You may prefer to use:

        <xsl:value-of select="normalize-space(.)"/>

in order to produce:

<p>This is a text with random breaklines</p>

That said, I don't see why you need to do anything at all. A simple:

        <xsl:value-of select="."/>

will result in:

<p> This is a
text with random
breaklines </p>

which any browser will render as:

enter image description here

If you are seeing a different result, then there must be some additional factor not mentioned in your question.

Related