Search code examples
xmlxmldom

Should consecutive whitespace in an XML file be ignored by an XML reader?


At my work we have our own XML classes which build a DOM, but I'm not sure how consecutive whitespace should be handled?

e.g.

<some-text>
Hello     World
</some-text>

When this is read into the DOM, should the text node include the consecutive whitespace inbetween Hello and World or just reduce it to one space?

Or should the XML source be written like this:

<some-text>
Hello &nbsp;&nbsp;&nbsp;&nbsp;World
</some-text>

or if not &nbsp; than perhaps &#32; ?


Solution

  • &nbsp; is a HTML entity and nothing to do with XML itself.

    To answer your question though, i would treat that as significant. Even the HTML DOM treats consecutive spaces as significant, it's just that it only visually renders one space. How it appears in the DOM and how it appears on your screen are two entirely different things.