How do I preserve things like tabs and carriage returns in text? when transforming XML to XHTML with XSL. For example:


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="article.xsl"?>
	<article name="Test Article">
		<heading>Test Article</heading>
		<subheading>Special Chars</subheading>
				a less than &lt;
				a greater than &gt; 
			<text type="code">
				some code.


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl=""> 

<xsl:template match="content">
	<xsl:apply-templates select="article" />

<xsl:template match="article">
	<h2><xsl:value-of select="heading" /></h2>
	<xsl:value-of select="subheading" />
	<xsl:for-each select="paragraph">
		<xsl:for-each select="text">
			<xsl:when test="@type = 'link'">
				<a href="{@href}" target="{@target}">
				<xsl:value-of select="." />
			<xsl:when test="@type = 'code'">
				<div class="code">
				<xsl:value-of select="." />
				<xsl:value-of select="." />


The carriage return between the text line:
a less than &lt;
a greater than &gt;

is lost when rendered in the browser, I sort of understand why that is, but I don't know how to apply XML or XSL to include/preserve or force the carriage return.

Recommended Answers

All 4 Replies

Is there a way to make substitutions? I know nothing of xslt, but you want to substitute a <br /> tag where ever the style sheet encounters the &gt;

If you are generating the xml yourself, you might try this:

      <td>a less than</td>
      <td>a greater than</td>

Hope this helps.. :sad:

Thanks guys.

I followed Daves links, it tells me XSL preserves whitespace by default. What I havn't mentioned is that in my particular scenario i'm using client javascript transformNode function and I can't be certain if that is doing what it should.

Anyway I have plenty of options to try now. If I nail it, i'll post the horrid details.

You need to replace the line carriage characters with <br/> tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.