XML non breaking space

2019-04-19 06:59发布

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>

标签: xml xslt
2条回答
▲ chillily
2楼-- · 2019-04-19 07:17

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.

查看更多
Anthone
3楼-- · 2019-04-19 07:26

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

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="..." ?>). Declaring UTF-8 is optional, as this is the default anyway.

In other words: There is no need to specially escape any character anywhere unless leaving it unescaped would break XML syntax rules (like < or & would).

You are of course free to escape any character you want.

These representations are equivalent as far as the resulting document is concerned:

<foo>&#x54;&#x65;&#x73;&#x74;&#xa0;&#x54;&#x65;&#x73;&#x74;</foo>

<foo>&#84;&#101;&#115;&#116;&#160;&#84;&#101;&#115;&#116;</foo>

<foo>Test&#160;Test</foo>

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

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.

查看更多
登录 后发表回答