Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keeping WhiteSpace in XML text element

Status
Not open for further replies.

batotoba

Programmer
Aug 14, 2006
5
0
0
FR
Hello,

I need to display the content of an xml text element in html via xslt.
I would like to know how I can do to display it exactly the way I want:
For example with text element parameters:
<parameters>
param1.1 param1.2
param2.1 param2.2 param2.3
param3.1
<parameters>
I get with xml spy:
param1.1 param1.2 param2.1 param2.2 param2.3 param3.1

I don't have the carriage return. Also how can I do if I need to display some
words in bold in my text element (for example 1.2). I cannot use <b> tag
in xml meaning the same than in html.

Thanks.
 
Your problem is not well stated. For example, I cannot determine what output you are expecting, or even its format. XSLT can output XML, HTML, or text. Which do you expect?

Also, without seeing your XSLT document, it will be difficult to guess what you might be doing.

Please provide better information.

Tom Morrison
 
Ok sorry.
My problem is for displaying the content of a text element
which contains text with whitespaces (tabulations, carriage return...)or some words in bold, italic...
In the example I gave
I would like the html output to be exactly as it appears in the xml element:

param1.1 param1.2
param2.1 param2.2 param2.3
param3.1

with the three carriage return but instead I get in the html output:

param1.1 param1.2 param2.1 param2.2 param2.3 param3.1

in one line. I don't know how I can do that in xslt as the text is in one element . I just have something like:

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

Thanks.
 
Your problem is probably the fact that browsers render whitespace as they see fit. In order to get an actual line break to render in a browser you must substitute a <br /> element for the line break.

What this means for your XSLT is that you will need to recursively apply a template on a string to make the substitution for an unknown number of line break characters.

Here is a stylesheet that will probably bring you closer to your goal:
Code:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<p>
<xsl:call-template name="insertNL">	
<xsl:with-param name="theString" select="."/>
</xsl:call-template>
</p>
</xsl:template>
<xsl:template name="insertNL">
<xsl:param name="theString"/>
<xsl:choose>
<xsl:when test="contains($theString,$newline)">
<xsl:value-of select="substring-before($theString,$newline)"/><br/><xsl:value-of select="$newline"/>
<xsl:call-template name="insertNL">
<xsl:with-param name="theString" select="substring-after($theString,$newline)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$theString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top