实体名称必须紧跟在“&"之后在实体引用中 [英] The entity name must immediately follow the '&' in the entity reference

查看:27
本文介绍了实体名称必须紧跟在“&"之后在实体引用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 *.xhtml 页面上放置一个 Packman 游戏.(我使用的是 jsf 2 和 primefaces 3.5)

I want to put a packman game on my *.xhtml page.(I am using jsf 2 and primefaces 3.5)

然而,

当我在 xhtml 中翻译"html 页面时,在此脚本中出现错误:

when I "translated" the html page in xhtml I get an error at this script:

    <script>

    var el = document.getElementById("pacman");

    if (Modernizr.canvas && Modernizr.localstorage && 
        Modernizr.audio && (Modernizr.audio.ogg || Modernizr.audio.mp3)) {
      window.setTimeout(function () { PACMAN.init(el, "./"); }, 0);
    } else { 
      el.innerHTML = "Sorry, needs a decent browser<br /><small>" + 
        "(firefox 3.6+, Chrome 4+, Opera 10+ and Safari 4+)</small>";
    }
  </script>

在线:

if (Modernizr.canvas && Modernizr.localstorage && 

我明白了:

实体名称必须紧跟在&"之后在实体参考.

The entity name must immediately follow the '&' in the entity reference.

知道如何解决这个问题吗?

Any idea how to fix that?

推荐答案

到目前为止发布的所有答案都提供了正确的解决方案,但是没有一个答案能够正确解释具体问题的根本原因.

All answers posted so far are giving the right solutions, however no one answer was able to properly explain the underlying cause of the concrete problem.

Facelets 是一种基于 XML 的视图技术,它使用 XHTML+XML 生成 HTML 输出.XML 有五个特殊字符,它们被 XML 解析器特殊处理:

Facelets is a XML based view technology which uses XHTML+XML to generate HTML output. XML has five special characters which has special treatment by the XML parser:

  • < 标签的开始.
  • > 标签的结尾.
  • " 属性值的开始和结束.
  • ' 属性值的替代开始和结束.
  • & 实体的开始(以 ; 结尾).
  • < the start of a tag.
  • > the end of a tag.
  • " the start and end of an attribute value.
  • ' the alternative start and end of an attribute value.
  • & the start of an entity (which ends with ;).

如果 & 后面没有 #(例如 &#160;, &#xA0; 等),XML 解析器隐式地寻找五个 预定义实体之一名称 ltgtampquotapos,或任何手动定义的实体名称.但是,在您的特定情况下,您使用 & 作为 JavaScript 运算符,而不是作为 XML 实体.这完全解释了您遇到的 XML 解析错误:

In case of & which is not followed by # (e.g. &#160;, &#xA0;, etc), the XML parser is implicitly looking for one of the five predefined entity names lt, gt, amp, quot and apos, or any manually defined entity name. However, in your particular case, you was using & as a JavaScript operator, not as an XML entity. This totally explains the XML parsing error you got:

实体名称必须紧跟在&"之后在实体引用中

The entity name must immediately follow the '&' in the entity reference

本质上,您在错误的地方编写了 JavaScript 代码,即 XML 文档而不是 JS 文件,因此您应该相应地转义所有 XML 特殊字符.& 必须转义为 &amp;.

In essence, you're writing JavaScript code in the wrong place, a XML document instead of a JS file, so you should be escaping all XML special characters accordingly. The & must be escaped as &amp;.

因此,在您的特定情况下,

So, in your particular case, the

if (Modernizr.canvas && Modernizr.localstorage && 

必须成为

if (Modernizr.canvas &amp;&amp; Modernizr.localstorage &amp;&amp;

使其对 XML 有效.

to make it XML-valid.

然而,这使得 JavaScript 代码更难阅读和维护.如果您想在 XML 文档的 JavaScript 代码中继续使用 & 而不是 &amp;,那么您应该将 JavaScript 代码放在字符数据中(CDATA) 块.因此,在 JSF 术语中,这将是:

However, this makes the JavaScript code harder to read and maintain. In case when you want to continue using & instead of &amp; in JavaScript code in a XML document, then you should be placing the JavaScript code in a character data (CDATA) block. Thus, in JSF terms, that would be:

<h:outputScript>
    <![CDATA[
        // ...
    ]]>
</h:outputScript>

XML 解析器会将块的内容解释为普通的";字符数据而不是 XML,因此将 XML 特殊字符按原样"解释.

The XML parser will interpret the block's contents as "plain vanilla" character data and not as XML and hence interpret the XML special characters "as-is".

但是,更好的是将 JS 代码放在它自己的 JS 文件中,该文件由 <script src> 包含,或者在 JSF 术语中,<h:outputScript>.

But, much better is to just put the JS code in its own JS file which you include by <script src>, or in JSF terms, the <h:outputScript>.

<h:outputScript name="onload.js" target="body" />

(注意target=body";这样JSF会在的最后自动渲染

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆