[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] line breaks in XML data


Subject: RE: [xsl] line breaks in XML data
From: "Matthieu Ricaud" <matthieu.ricaud@xxxxxxx>
Date: Fri, 21 Oct 2005 14:41:18 +0200

I used to have the same problem I think.
Tell me if I'm wrong : what you want is to preserve line breaks on the final
HTML output.
In html, lines breaks in the source are ignored : you need a <br/> element
so that the break can been seen in your browser (only textarea preserve the
source's line breaks ).
Then you need a template which replace every line break characters ("&#xA;")
by a <br/> element.

This couple of templates make this. (I don't remembre who I have it from)

	<xsl:template name="lf2br">
		<!-- import $StringToTransform -->
		<xsl:param name="StringToTransform"/>
		<xsl:choose>
			<!-- string contains linefeed -->
			<xsl:when test="contains($StringToTransform,'&#xA;')">
				<!-- output substring that comes before the first linefeed -->
				<!-- note: use of substring-before() function means        -->
				<!-- $StringToTransform will be treated as a string,       -->
				<!-- even if it is a node-set or result tree fragment.     -->
				<!-- So hopefully $StringToTransform is really a string!   -->
				<xsl:value-of select="substring-before($StringToTransform,'&#xA;')"/>
				<!-- by putting a 'br' element in the result tree instead  -->
				<!-- of the linefeed character, a <br> will be output at   -->
				<!-- that point in the HTML                                -->
				<br/>
				<!-- repeat for the remainder of the original string -->
				<xsl:call-template name="lf2br">
					<xsl:with-param name="StringToTransform">
						<xsl:value-of select="substring-after($StringToTransform,'&#xA;')"/>
					</xsl:with-param>
				</xsl:call-template>
			</xsl:when>
			<!-- string does not contain newline, so just output it -->
			<xsl:otherwise>
				<xsl:value-of select="$StringToTransform"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

I defined another template to call like this :
			<xsl:call-template name="CopyWithLineBreaks">
				<xsl:with-param name="string" select="some_Xpath"/>
			</xsl:call-template>

	<xsl:template name="CopyWithLineBreaks">
		<xsl:param name="string"/>
		<xsl:variable name="Result">
			<xsl:call-template name="lf2br">
				<xsl:with-param name="StringToTransform" select="$string"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:copy-of select="$Result"/>
	</xsl:template>

Hope this help !
Matthieu.
-----Message d'origine-----
De : Emmanouil Batsis [mailto:Emmanouil.Batsis@xxxxxxxxxxx]
Envoyi : vendredi 21 octobre 2005 14:18
@ : xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Objet : Re: [xsl] line breaks in XML data


Jim Gay wrote:

> forgive my novice ineptitude.
>
> I am working on a project where we would like to preserve line breaks
> that are entered into our XML database.


In the root of your XML documents add xml:space="preserve". That should
force the XML parser to keep whitespace.


> We are doing our XSL transformations with version 1.
> If I use my XSLT to output/display this data into a <textarea>
> element, the line breaks found in the database are there when displayed.
> If I output to a simple <p> element, the line breaks are gone.


Use a <pre> instead of a <p>. You got me all confused now :-P

Manos


Current Thread
Keywords