A document should open with an XML declaration, <?xml ... ?>, which has three pseudo-attributes:
version: write 1.0. XML 1.1 (2004) added post-Unicode-3.2 characters and mainframe line endings, but few parsers implement it, and XML 1.0 Fifth Edition (2008) adopted its liberal naming rules anyway.
encoding: how bytes map to characters. Without it or a byte order mark (BOM), a parser assumes UTF-8.
standalone: yes promises that no external DTD declarations change what the application sees.
The declaration must come first in the file (even a leading space is a fatal error), and it must tell the truth. A classic ingestion bug is a Latin-1 export that claims to be UTF-8:
# The byte 0xE9 is "é" in ISO-8859-1 (Latin-1) but is not valid UTF-8 on its own
printf '<?xml version="1.0" encoding="UTF-8"?>\n<title>Caf\xe9</title>\n' > liar.xml
printf '<?xml version="1.0" encoding="ISO-8859-1"?>\n<title>Caf\xe9</title>\n' > honest.xml
xmllint --noout liar.xml
xmllint --encode UTF-8 honest.xml > fixed.xml
cat fixed.xml; file fixed.xmlliar.xml:2: I/O error : Invalid bytes in character encoding
Bytes: 0xE9 0x3C 0x2F 0x74
<title>Caf
^
<?xml version="1.0" encoding="UTF-8"?>
<title>Café</title>
fixed.xml: XML 1.0 document, Unicode text, UTF-8 textThe honest file parses, and --encode UTF-8 rewrites it as UTF-8 with a matching declaration. Normalize every inbound feed to UTF-8 at the edge of the pipeline. (To read the declaration at all, a parser first guesses the encoding from a BOM or the bytes of <?xm, as XML 1.0's Appendix F describes.)
Document Declaration
An XML document should begin with an XML declaration, denoted by <?xml …… ?>. The three possible attributes are:- version: There are currently version 1.0 and version 1.1. Version 1.1 has not been widely implemented. The main changes are to enable the use of line-ending characters used on EBCDIC 30 platforms, and the use of scripts and characters absent from Unicode 3.2. XML 1.1 came out of a desire to support all the world's languages, and is generally not used.
- encoding: Specifies the encoding of the document.
- standalone: The valid values are 'yes' and 'no'. A standalone XML document is one that tells the XML processor to ignore any DTDs. 'yes' indicates that there are no external markup declarations which affect the information passed from the XML processor to the application.
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<bookslist>
</bookslist>