Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML non breaking space

Tags:

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>
like image 347
jlp Avatar asked Nov 14 '13 10:11

jlp


People also ask

What is non-breaking space in XML?

In word processing and digital typesetting, a non-breaking space, , also called NBSP, required space, hard space, or fixed space (though it is not of fixed width), is a space character that prevents an automatic line break at its position.

Which code means non-breaking space?

Alternatively called a fixed space or hard space, NBSP (non-breaking space) is used in programming and word processing to create a space in a line that cannot be broken by word wrap. With HTML, &nbsp; lets you create multiple spaces that are visible on a web page and not only in the source code.

What does a nonbreaking space do?

A nonbreaking space is the same width as a word space, but it prevents the text from flowing to a new line or page. It's like invisible glue between the words on either side.


2 Answers

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.

like image 57
Tomalak Avatar answered Oct 07 '22 01:10

Tomalak


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.

like image 41
ITI Avatar answered Oct 07 '22 01:10

ITI