tags:

views:

525

answers:

4

Part of the XML:

<text><b>Title</b> <b>Happy</b></text>

In my XSL I have:

<xsl:value-of select="text" disable-output-escaping="yes" />

My output becomes

**TitleHappy**

My spacing went missing - there's supposed to be a space between </b> and <b>.

I tried normalize-space(), it doesn't work.

Any suggestions? Thanks!

+1  A: 

if you want whitespace from an xsl, use: <xsl:text> </xsl:text>

whitespace is only preserved if its recognized as a text node (ie: " a " both spaces will be recognized)

whitespace from the orignal source xml has to be preserved by telling the parser (for example)

parser.setPreserveWhitespace(true);

Niko
So I supposed that the space in between the <b> and </b> is not preserved as it is not recognised as a text node? Anyway to go about preserving it and showing it as output ?
i was mistook your source as xsl (so the xsl:text method wont work). i am afraid that the xml parser removes the whitespace in your source-xml. if you add any other non-whitespace char it will be recognized as textnode and thus preserve the whitespace (at least once). I would take a close look to the options of your xml parser and make him preserve whitespace. Otherwise a   (or   - nonbreaking space) is the only thing that would help.
Niko
A: 

As your outputting HTML you could substitute your space with a non-breaking space  

MattH
Thanks. I am currently using your suggestion.
A: 

Do you have any control over the generation of the original XML? If so, you could try adding xml:space="preserve" to the text element which should tell the processor to keep the whitespace.

<text xml:space="preserve"><b>Title</b> <b>Happy</b></text>

Alternatively, try looking at the "xsl:preserve-space" element in XSLT.

<xsl:preserve-space elements="text"/>

Although I have never used this personally, it might of some help. See W3Schools for more information.

Tim C
I have no control over the original XML. =(I tried preserve-space, it doesn't work. Found out it is a default setting anyway.The space disappeared only when the bold tags are used.<text>Title Happy</text> still return me Title HappyWeird.
A: 

Hello, thank you for everyone's input. Currently I am using MattH suggestion which is to test for space and substitue to non-breaking space. Another method I thought of is to test for "</b> <b>" and substitue with " </b><b>". The space contain within a bold tags are actually output. Both methods worked. Don't know what the implications are though. And I still can't figure out why the spacing is removed when it is found between 2 seperate bold tags.