Javascript 解析 XHTML 时出错:元素的内容必须由格式正确的字符数据或标记组成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4338538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:58:09  来源:igfitidea点击:

Error parsing XHTML: The content of elements must consist of well-formed character data or markup

javascriptjsfjsf-2xhtmlfacelets

提问by luciaengel

As an extension of this question, I'm trying to insert Javascript to a <h:commandButton />'s onclickproperty as actionis already rendering an ajax table.

作为这个问题的扩展,我正在尝试将 Javascript 插入到 a<h:commandButton />onclick属性中,因为action它已经呈现了一个 ajax 表。

What I want to do: Get the selected items in a list box and turn them into parameters to be used in a JSF FileServlet. i.e. para2=value1&param=value2&param=value3

我想要做什么:在列表框中获取所选项目并将它们转换为要在 JSF 中使用的参数FileServlet。IEpara2=value1&param=value2&param=value3

Here's what I have:

这是我所拥有的:

<script type ="text/javascript">
function myScript() {
    var box = document.getElementbyId('myForm:box');
    var length = box.options.length;
    var paramstring = "";
    for (var i = 0; i < length; i++) {
        if (i != (length - 1) {
            if (box.options[i].selected) {
                paramstring = paramstring + "param=" + box.options[i].value + "&amp;";
            }
        } else {
            paramstring = paramstring + "param=" + box.options[i].value;
        }
    }
    if (document.getElementById('myForm:checkbox').checked) {
        window.location='fileServlet? + paramstring;
    }
}
</script>  

What I get when page is loaded: javax.servlet.ServletException: Error Parsing /page.xhtml: Error Traced[line:15] The content of elements must consist of well-formed character data or markup.

页面加载时我得到了什么: javax.servlet.ServletException: Error Parsing /page.xhtml: Error Traced[line:15] The content of elements must consist of well-formed character data or markup.

What doesn't trigger exception:

什么不会触发异常:

<script type ="text/javascript">
function myScript() {
    var box = document.getElementbyId('myForm:box');
    var length = box.options.length;
    var paramstring = "";

    if (document.getElementById('myForm:checkbox').checked) {
        window.location='fileServlet? + paramstring;
    }
}
</script> 

As soon as I add in for (var i = 0; i < length; i++)or even for (var i = 0; i < 10; i++)the page wouldn't load. Why does it not like the for loop?

一旦我添加,for (var i = 0; i < length; i++)甚至for (var i = 0; i < 10; i++)页面都不会加载。为什么它不喜欢 for 循环?

回答by BalusC

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:

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

  • <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 ;).
  • <标签的开始。
  • >标签的结尾。
  • "属性值的开始和结束。
  • '属性值的替代开始和结束。
  • &实体的开头(以 结尾;)。

In case of <, the XML parser is implicitly looking for the tag name and the end tag >. 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:

在 的情况下<,XML 解析器隐式地查找标记名称和结束标记>。但是,在您的特定情况下,您使用的<是 JavaScript 运算符,而不是 XML 实体。这完全解释了您遇到的 XML 解析错误:

The content of elements must consist of well-formed character data or markup.

元素的内容必须由格式良好的字符数据或标记组成。

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 &lt;.

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

So, essentially, the

所以,本质上,

for (var i = 0; i < length; i++) {

must become

必须成为

for (var i = 0; i &lt; length; i++) {

to make it XML-valid.

使其 XML 有效。

However, this makes the JavaScript code harder to read and maintain. As stated in Mozilla Developer Network's excellent document Writing JavaScript for XHTML, you should be placing the JavaScript code in a character data (CDATA) block. Thus, in JSF terms, that would be:

然而,这使得 JavaScript 代码更难阅读和维护。正如 Mozilla Developer Network 的优秀文档为 XHTML 编写 JavaScript 中所述,您应该将 JavaScript 代码放置在字符数据 (CDATA) 块中。因此,在 JSF 术语中,这将是:

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

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".

XML 解析器将块的内容解释为“普通”字符数据而不是 XML,因此解释 XML 特殊字符“原样”。

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>.

但是,更好的是只是把自己的JS文件中的JS代码,您包括由<script src>,或者在JSF方面,<h:outputScript>

<h:outputScript name="functions.js" target="head" />

This way you don't need to worry about XML-special characters in your JS code.

这样您就无需担心 JS 代码中的 XML 特殊字符。

See also:

也可以看看:

回答by MikeR

I ran across this post today as I was running into the same issue and had the same problem of the javascript not running with the CDATA tags listed above. I corrected the CDATA tags to look like:

我今天遇到了这篇文章,因为我遇到了同样的问题,并且 javascript 没有运行上面列出的 CDATA 标签。我更正了 CDATA 标签,看起来像:

<script type="text/javascript">
//<![CDATA[ 

your javascript code here

//]]>
</script>

Then everything worked perfectly!

然后一切正常!

回答by Jad B.

Sometimes you will need this :

有时你会需要这个:

 /*<![CDATA[*/
 /*]]>*/

and not only this :

不仅如此:

 <![CDATA[
 ]]>

回答by GarethReid

I had a git conflict left in my workspace.xml i.e.

我的workspace.xml 中有一个git 冲突,即

<<<<———————HEAD

which caused the unknown tag error. It is a bit annoying that it doesn't name the file.

这导致了未知的标签错误。它没有命名文件有点烦人。

回答by PedroPK

I solved this converting the JSP from XHTML to HTML, doing this in the begining:

我解决了将 JSP 从 XHTML 转换为 HTML 的问题,一开始就这样做:

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
...