Error Parsing Xhtml: the Content of Elements Must Consist of Well-Formed Character Data or Markup

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

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

In case of <, the XML parser is implicitly looking for the tag name and the end tag >. However, in your particular case, you were using < as a JavaScript operator, not as an XML entity. This totally explains the XML parsing error you got:

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

So, essentially, the

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

must become

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

to make it XML-valid.

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:

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

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="functions.js" target="head" />

This way you don't need to worry about XML-special characters in your JS code. Additional advantage is that this gives the browser the opportunity to cache the JS file so that average response size is smaller.

See also:

  • The entity name must immediately follow the '&' in the entity reference
  • Is it possible to use JSF+Facelets with HTML 4/5?
  • How to reference CSS / JS / image resource in Facelets template?
  • Writing JavaScript for XHTML

javascript error The content of elements must consist of well-formed character data or markup in blogger

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

you may not use a < inside inline scripts because the parser thinks of it to be a tag beginning.

Content of elements must consist of well-formed mark-up

Does this piece of JavaScript happen to live in a <script> tag in an XHTML page? If so, move it to an external JS file, or use a CDATA section in the script tag.

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

Sorry, but I guess you have to pass string to document.write(), and also use appendChild() instead of it. I mean, you are trying to do this:

document.write("<script src=\/feeds/posts/default/?max-results=+numposts4+&orderby=published&alt=json-in-script&callback=owlcontent;\><\/script>");

but instead, you can do this (better practise):

let scriptTag = document.createElement("script");
scriptTag.src = "path/to/your/script.js";
document.body.appendChild(scriptTag);

Google analytics experiment causing well-formed character data or markup error

If CDATA doesn't work out, because Google is jerking about it, then there are basically 2 other solutions:

  1. Manually escape XML special characters to make the whole JS code syntactically XML valid. Replace < by <, & by &, > by >, etc.

  2. Put JS code in its own JS file and include by <script src>. Not sure if Google Analytics Validator eats that. Edit: thus not.

I'd at least report an issue to Google Analytics guys that their validator is severely broken.

See also:

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

Getting a XHTML parsing error when using JavaScript in Facelets

Try placing the javascript code within a CDATA tag

<script type="text/javascript">
<![CDATA[
$(document).ready(function() {
....
}
]]>
</script>


Related Topics



Leave a reply



Submit