XML文件中nbsp的产生和问题解决

1.   nbsp usage, the definitive, full answer (and you thought it was 42?) 2.   nbsp doesn't work 3.   nbsp in output 4.   Another explanation 5.   nbsp, why doesn't it work

1.

nbsp usage, the definitive, full answer (and you thought it was 42?)

Jeni Tennison.


  > Could somebody explain to my WHY '&' translates to '&'
  > but ' ' doesn't change at all?

Let's consider this simple stylesheet:

  <?xml version="1.0" encoding="ISO-8859-1"?>
  <xsl:stylesheet version="1.0"
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  <xsl:template match="/">
    <html>
      <head><title>Test</title></head>
      <body>
        <p>Non-breaking&amp;nbsp;space</p>
        <p>Non-breaking&#160;space</p>
      </body>
    </html>
  </xsl:template>
                  
  </xsl:stylesheet>

This stylesheet is stored on the hard disk as a series of bytes. The bytes match characters according to the ISO-8859-1 encoding (see the encoding pseudo-attribute on the XML declaration?).

When the XML parser reads in this as an XML document, it decodes the bytes into Unicode characters. It also parses the document, recognising things like start tags (e.g. <p>), built-in entity references (e.g. &amp;) and character references (e.g. &#160;).

The parser knows that &amp; stands for an & character (because it knows XML) and knows that &#160; stands for a non-breaking space character (because it knows XML and Unicode).

The parser reports to the XSLT processor when elements occur and what characters text is made up of, but doesn't report whether a particular character was originally serialized as the plain character (an actual space character), an entity reference or a character reference.

As far as an XSLT processor is concerned, therefore, the following elements in the stylesheet (or in an XML source document) would all be reported as *exactly* the same (a p element containing a text node whose string value is a double-quote character):

    <p>"</p>
    <p>&#34;</p>
    <p>&#x22;</p>
    <p>&quot;</p>
    <p><![CDATA["]]></p>

The two p elements serialized in the stylesheet, look like:

    <p>Non-breaking&amp;nbsp;space</p>
    <p>Non-breaking&#160;space</p>

For the first p element, the XML parser reports the string (here containing no escaping of any kind - every character is a literal character):

Non-breaking&nbsp;space

For the second p element, the XML parser reports the string (here containing an underscore character as a stand-in for a non-breaking space, since you can't see non-breaking spaces in emails):

Non-breaking_space

The XSLT processor builds a result tree from the stylesheet, which contains these text n

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值