RE: [xsl] creating new lines in textarea input for HTML

Subject: RE: [xsl] creating new lines in textarea input for HTML
From: "Matthieu Ricaud" <matthieu.ricaud@xxxxxxx>
Date: Fri, 27 Jan 2006 19:24:39 +0100
hello,

i used to have found this template while surching for a similar problem :

	<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>

Whenever i want to diplay line breaks of a string with a <br/> html element
i just do :

			<xsl:call-template name="CopyWithLineBreakBR">
				<xsl:with-param name="string" select="'my breaked
				string'"/>
			</xsl:call-template>


	<xsl:template name="CopyWithLineBreakBR">
		<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,

matt

-----Message d'origine-----
De : Stuart Zakon [mailto:sz@xxxxxxxxxxxxxxxxxxx]
Envoyi : vendredi 27 janvier 2006 19:02
@ : xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Objet : [xsl] creating new lines in textarea input for HTML


The <textarea> input in HTML will take a value that has multiple lines CR/LF
but will not
preserve the line feeds (treated as whitespace).

On the other hand, if I add these using <xsl:text>&#x0a;</xsl:text>, they
show up fine in
the textarea.

So the challenge is, how do I break up the string using XSLT and put in the
above?

I though string-before() would work but realized that it is treating
'&#x0a;' as literal
text.  Is there another way to do this?

Thanks,
Stuart Zakon

Current Thread