jlp
jlp

Reputation: 10358

XML non breaking space

I am using XLST files to transform XML to XML.

What are valid representation of space?

<xsl:text> </xsl:text>
<xsl:text>&nbsp;</xsl:text>
<xsl:text>&#160;</xsl:text>

Upvotes: 23

Views: 24713

Answers (2)

Tomalak
Tomalak

Reputation: 338376

XML does not have any named entities besides &lt;, &gt;, &amp;, &quot;, and &apos;.

All other characters can be represented verbatim, given that you declared the right encoding in the XML declaration (e.g. <?xml version="1.0" encoding="..." ?>), and actually saved the XML file in that encoding. Declaring UTF-8 is optional, as this is the default for XML.

The "right" encoding is any encoding that contains all the characters you want to use. Choosing Unicode is both popular and practical, but XML does not care as long as you've declared it properly.

Any character the chosen character set supports you can use as-is, with the exception of those that have special meaning in XML (<, >, or &, which must always be escaped, and ', or ", which only must be be escaped in certain situations). All other characters can be escaped, but you don't need to.

To make a point, these representations are 100% equivalent in terms of the resulting document (i.e. the object you get after an XML parser has read the file):

<foo>Test Test</foo>          <!-- unescaped - given that the " " really is char code 160 -->

<foo>Test&#160;Test</foo>     <!-- partially escaped -->

<foo>&#84;&#101;&#115;&#116;&#160;&#84;&#101;&#115;&#116;</foo>   <!-- decimal escaped -->

<foo>&#x54;&#x65;&#x73;&#x74;&#xa0;&#x54;&#x65;&#x73;&#x74;</foo> <!-- hex escaped -->

The non-breaking space is in no way special or different from, say, the letter "T". For convenience when editing the XML file with a text editor, you might want to choose the escaped form, but there is no technical requirement to do that.


Note that you can declare custom named entities (like &nbsp;) using a DOCTYPE.

<!DOCTYPE xsl:stylesheet [
   <!ENTITY nbsp "&#160;">
]>

But given the fact that XML accepts any character that's hardly ever necessary. Especially not when you create the document using a proper tool, like a DOM API.

Upvotes: 26

ITI
ITI

Reputation: 61

As it relates to the question, add all the entities that are causing parse errors to the DOCTYPE of your *.xls style sheet.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
   <!ENTITY nbsp "&#160;">
]>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Now you can use &nbsp; as you would normally.

Upvotes: 6

Related Questions