Skip to main content

XML error · entity

Unescaped ampersand (EntityRef: expecting ';')

A bare & appeared in content. XML reads it as the start of an entity reference and expects a name and a semicolon.

EntityRef: expecting ';' (line 2)

What it means

The ampersand is the character that breaks real documents. XML treats & as the opening of an entity reference, so it must be written & whenever it means a literal ampersand.

This is the single most common failure in generated sitemaps and feeds, because URLs with query strings contain ampersands and string interpolation does not escape them. URL-encoding does not help: %26 is for URLs, & is for XML, and a URL inside XML needs both layers respected.

What usually causes it

  • An unescaped & in a URL query string: ?a=1&b=2 instead of ?a=1&b=2.
  • Company names and titles containing & written literally.
  • HTML built by string concatenation and dropped into a feed without escaping.
  • Double-escaping fixes applied on top of already-escaped output, then partially reverted.

How to fix it

  • Escape the five predefined entities: & < > " '.
  • Escape URLs when writing them into XML, even though they are already URL-encoded.
  • Wrap HTML bodies in CDATA rather than escaping them character by character.
  • Build XML with a serializer; every one of them escapes content for you.

The same error elsewhere

Different parsers, same defect. If you arrived with one of these messages, you are in the right place.

  • libxml2 (bare & with no name)

    xmlParseEntityRef: no name
  • Xerces (Java)

    The reference to entity "page" must end with the ';' delimiter.
  • expat (Python)

    not well-formed (invalid token): line 2, column 40
  • .NET System.Xml

    An error occurred while parsing EntityName.

Before and after

Both snippets are re-checked by the test suite against the real parser: the first is confirmed to produce the exact error above, the second to parse cleanly.

Fails
<url>
  <loc>https://example.com/search?q=xml&page=2</loc>
</url>
Parses
<url>
  <loc>https://example.com/search?q=xml&amp;page=2</loc>
</url>

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.