The Entity Name Must Immediately Follow the '&' in the Entity Reference

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

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 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 & which is not followed by # (e.g.  ,  , 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

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, in your particular case, the

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

must become

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

to make it XML-valid.

However, this makes the JavaScript code harder to read and maintain. In case when you want to continue using & instead of & 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>

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="onload.js" target="body" />

(note the target="body"; this way JSF will automatically render the <script> at the very end of <body>, regardless of where <h:outputScript> itself is located, hereby achieving the same effect as with window.onload and $(document).ready(); so you don't need to use those anymore in that script)

This way you don't need to worry about XML-special characters in your JS code. As an additional bonus, this gives you the opportunity to let the browser cache the JS file so that total response size is smaller.

See also:

  • Error Parsing /page.xhtml: Error Traced[line: 42] The entity "nbsp" was referenced, but not declared
  • Is it possible to use JSF+Facelets with HTML 4/5?
  • How to reference CSS / JS / image resource in Facelets template?

Why must the entity name immediately follow the '&' in the entity reference appearing for the XML?

You have an unescaped & in there, just like @chrylis says the message says. ;-)

Change Business IAD & Business CPE to Business IAD & Business CPE.

See also: What characters do I need to escape in XML documents?

Entity name must Immediately follow the '&' in the entity reference

The & symbol is an entity specifier in XML. If you want to put a literal ampersand in the XML, you will need to use &, which is the standard entity for an ampersand.

Getting a The entity name must immediately follow the '&' in the entity reference error in java, but I dont have any ampersands in my xml file

Yes, you do have an ampersand:

<item><title>Pad & Quill

Try

<item><title>Pad & Quill

Also, this was probably a typo: <\item><title>Deal of the Day which you can correct by removing the backslash.

Using && in EL results in error: The entity name must immediately follow the '&' in the entity reference

You seem to be using Facelets (which is perfectly fine). It's however a XML based view technology. Everything which you write in Facelets has to be syntactically valid XML. The & is in XML a special character denoting the start of an entity like &, <, >,  , etc.

If you would like to represent the & as-is in XML, then you'd have to replace it by &.

<h:outputText value="#{(sel.description !=null) && (sel.description !='') ? sel.description : 'Empty description'} - "/>

However, that's not pretty readable, you would rather like to use the alternative EL operator and for this (see also operators in EL for an overview of all available operators in EL):

<h:outputText value="#{(sel.description !=null) and (sel.description !='') ? sel.description : 'Empty description'} - "/>

All with all, this is in turn pretty clumsy as there's a simpler empty keyword for the purpose of testing both nullness and emptiness. This can in your particular case be used as:

<h:outputText value="#{not empty sel.description ? sel.description : 'Empty description'} - "/>

or

<h:outputText value="#{!empty sel.description ? sel.description : 'Empty description'} - "/>

or

<h:outputText value="#{empty sel.description ? 'Empty description' : sel.description} - "/>


Related Topics



Leave a reply



Submit