# The processing instruction target matching "[xX][mM][lL]" is not allowed

Source: https://xmlvalid.com/errors/processing-instruction-target-matching-xml-is-not-allowed

> An XML declaration (…?) appears somewhere other than the very start.

The message, as Java reports it: `The processing instruction target matching "[xX][mM][lL]" is not allowed.`

## Why it happens

- Whitespace or a blank line before `<?xml`, often from a template, a script that prints a newline first, or a string built with a leading line break.
- Two documents concatenated, so the second declaration lands in the middle.
- A byte order mark or other bytes before the declaration that the reader passed on as text.

## How to fix it

- Make `<?xml` the first characters of the input: trim the string before parsing, or remove the second declaration when documents are combined.
- 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 [XML declaration allowed only at the start of the document](https://xmlvalid.com/errors/xml-declaration-allowed-only-at-the-start): `XML declaration allowed only at the start of the document`

## Example that fails

```xml

<?xml version="1.0"?>
<note/>
```

## Fixed

```xml
<?xml version="1.0"?>
<note/>
```
