Mysterious space in HTML output after XSL conversion

Nice teamwork by Clay Hellberg and Brandon L Ibach ("&amp;nbsp;" was the problem) Last Updated: 2006-09-12

=Question from N.N.=

I'm having a really strange problem after converting an XML document to HTML using XSL.

Whenever I select certain XML title elements in the stylesheet (such as a procedure title), a space appears before the title in the HTML output. This does not happen with all titles in our XML document. (Process titles do not have a leading space in HTML output.) In addition, none of the titles have a leading space in the XML document.

For example, this:

&lt;li>

yields the following text with a leading space in output:

* Procedure With Subprocedres Title

I wrote some JavaScript to get rid of the leading space because I could not get rid of it using any xslt/xpath functions, such as normalize-space:

function trimLeadingSpace (str) { var leadingSpace= / / result = str.replace(leadingSpace, "") }  trimLeadingSpace('');document.write(result)

However, the JavaScript did not work until I copied the leading space in the HTML text and pasted it in the applicable JavaScript code:

var leadingSpace= / /

Then the javascript worked and the leading space went away from the title.

=Clay Hellberg answers=

Looking at the string in Emacs' hex mode, the space there is 0xa0; better known as. It is probably there in the original XML before the transformation, and it's not recognized as ignorable whitespace w.r.t. normalize-space. To get rid of it, you can use the translate function, something like this:

&lt;li>

See if that works better for you.

=Brandon L Ibach adds=

Good call, Clay. However, I suspect it might be safer to convert the NBSP to a plain space, then use normalize-space (which only recognizes space, tab, CR and LF, but not NBSP, as whitespace) to get rid of extraneous spaces. That way, if you end up with NBSPs separating words, you won't accidentally join them together.

&lt;li>