# The entity was referenced, but not declared

Source: https://xmlvalid.com/errors/entity-was-referenced-but-not-declared

> The document uses a named entity, like &amp;nbsp;, that XML does not define.

The message, as Java reports it: `The entity "nbsp" was referenced, but not declared.`

## Why it happens

- HTML entities in XML: `&nbsp;`, `&copy;`, `&eacute;`. XML defines only `&lt;`, `&gt;`, `&amp;`, `&quot;` and `&apos;`.
- An entity declared in a DTD that is not read, for example because external DTD loading is turned off.

## How to fix it

- Replace the entity with the character itself or a numeric reference (`&#160;` for a non-breaking space). When the XML is built from HTML text, convert the entities before writing it.
- Java's schema validation (`javax.xml.validation.Validator`, and JAXB or Spring on top of it) throws at the first error unless you set an `ErrorHandler` whose `error()` records the `SAXParseException` and carries on; each one has `getLineNumber()` and `getColumnNumber()`. A well-formedness error ends parsing whatever the handler does.

## The same problem in xmlvalid

libxml2, which xmlvalid validates with, reports it as [Entity not defined](https://xmlvalid.com/errors/entity-not-defined): `Entity 'nbsp' not defined`

## Example that fails

```xml
<p>Price:&nbsp;10</p>
```

## Fixed

```xml
<p>Price:&#160;10</p>
```
