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

时间:2022-07-28 16:15:15

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

作为这个问题的扩展,我试图将Javascript插入 的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中使用的参数。即para2 = 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:错误解析/page.xhtml:错误跟踪[第15行]元素的内容必须由格式良好的字符数据或标记组成。

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?

一旦我加入(var i = 0; i ;>

4 个解决方案

#1


85  

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.
  • <标签的开头。< p>

  • > 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解析错误:< p>

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代码,而不是JS文件,因此您应该相应地转义所有XML特殊字符。 <必须转义为<。< p>

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的优秀文档Write JavaScript for XHTML所述,您应该将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解析器将块的内容解释为“普通的vanilla”字符数据而不是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文件中,该文件由

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

#2


17  

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:

今天我遇到了这个帖子,因为我遇到了同样的问题,并且遇到了与上面列出的CDATA标签无法运行的javascript相同的问题。我更正了CDATA标签,如下所示:

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

your javascript code here

//]]>
</script>

Then everything worked perfectly!

然后一切都很完美!

#3


5  

Sometimes you will need this :

有时你会需要这个:

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

and not only this :

而且不仅如此:

 <![CDATA[
 ]]>

#4


-3  

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

#1


85  

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.
  • <标签的开头。< p>

  • > 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解析错误:< p>

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代码,而不是JS文件,因此您应该相应地转义所有XML特殊字符。 <必须转义为<。< p>

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的优秀文档Write JavaScript for XHTML所述,您应该将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解析器将块的内容解释为“普通的vanilla”字符数据而不是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文件中,该文件由

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

#2


17  

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:

今天我遇到了这个帖子,因为我遇到了同样的问题,并且遇到了与上面列出的CDATA标签无法运行的javascript相同的问题。我更正了CDATA标签,如下所示:

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

your javascript code here

//]]>
</script>

Then everything worked perfectly!

然后一切都很完美!

#3


5  

Sometimes you will need this :

有时你会需要这个:

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

and not only this :

而且不仅如此:

 <![CDATA[
 ]]>

#4


-3  

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