The grammar of XML 1.0, fifth edition. Numbers in square brackets are the specification’s production numbers. The rules in the specification are in EBNF; here they are in words.
The document [1]
A document is a prolog, exactly one root element, then any number of comments, processing instructions and white space. The prolog [22] is an optional XML declaration, comments, processing instructions and white space, an optional document type declaration, and more of the same. Text outside the root element is not allowed.
The XML declaration [23]
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>. It must be
the very first thing in the file, in lower case, with the attributes in that order.
| Part | Production | Rule |
|---|---|---|
version | [24] [26] | Required. 1. followed by digits; 1.0 or 1.1 |
encoding | [80] [81] | Optional. A letter, then letters, digits, ., _ and - |
standalone | [32] | Optional. yes or no; see the DTD page |
Without a declaration a document is XML 1.0, in UTF-8 or UTF-16 by its byte order mark.
Characters and names
| Production | Allowed |
|---|---|
| [2] Char | #x9, #xA, #xD, #x20–#xD7FF, #xE000–#xFFFD, #x10000–#x10FFFF |
| [3] S (white space) | #x20, #x9, #xD, #xA, one or more |
| [4] NameStartChar | :, A–Z, _, a–z, #xC0–#xD6, #xD8–#xF6, #xF8–#x2FF, #x370–#x37D, #x37F–#x1FFF, #x200C–#x200D, #x2070–#x218F, #x2C00–#x2FEF, #x3001–#xD7FF, #xF900–#xFDCF, #xFDF0–#xFFFD, #x10000–#xEFFFF |
| [4a] NameChar | NameStartChar, -, ., 0–9, #xB7, #x300–#x36F, #x203F–#x2040 |
| [5] Name | NameStartChar then NameChars |
| [7] Nmtoken | One or more NameChars |
Names starting with xml in any case are reserved. Colons are allowed by the
grammar but are used by namespaces.
Elements and attributes
An element [39] is a start tag [40], content [43], and an end tag [42]; or an empty-element
tag [44], <name/>. Content is character data, elements, references,
CDATA sections, comments and processing instructions, freely mixed.
- Case: names are case sensitive, so
<A>and</a>do not match. - Attributes [41] are
name="value"orname='value', separated by white space. A name may appear only once in a tag. A value may not contain<or an unescaped ampersand, or its own quote. - Character data [14] is text without
<or&. The sequence]]>may not appear in it.
CDATA sections [18–21]
<![CDATA[ … ]]> holds text in which markup is not recognised, ending
at the first ]]>. It can appear only inside an element.
Processing instructions [16] [17]
<?target data?>, with a target that is a Name, not xml in
any case. Instructions are for the application named by the target, for example
<?xml-stylesheet href="s.css"?>, and are not part of the character
data.
White space, line ends and language
- Line ends (2.11): the parser turns CR LF and lone CR into LF before anything else, so documents can use any platform’s convention.
xml:space(2.10):"preserve"asks applications to keep the white space in an element;"default"means their default handling. It applies to the element’s content and inherits downward.xml:lang(2.12): the language of the element’s content, a BCP 47 tag; an empty value undeclares it. Both attributes must be declared if validated.
Comments [15]
<!-- … -->. The text may not contain--, so a comment may not end--->. A parser may or may not pass comments on to the application.