Basic XML rules: well-formed and valid XML

Well-formed vs valid XML

XML documents are essentially text documents containing data and markup. Data can be text or numeric. Markup is the tags used to add structure and semantic information. XML processing software expects  basic rules to be followed that distinguishes data from markup. If you are familiar with HTML, XML will seem quite familiar as it uses a similar markup system.

All XML documents have to conform to a set of rules that were developed by the World Wide Web Consortium (W3C) a community of organisations that develop web standards for core languages of the WWW (including HTML, CSS and XML). The rules for XML documents are described in the XML specification (https://www.w3.org/XML/).  XML processing software expects XML documents to be structured following these rules, and any divergence causes what is known as a parsing or syntax error.

W3Schools identifies five key rules for XML syntax. Documents that conform to these rules are called Well-Formed:

    1. XML documents must have a root element
    2. XML elements must have a closing tag
    3. XML tags are case sensitive
    4. XML elements must be properly nested
    5. XML attribute values must be quoted

    Valid XML, on the other hand, is a document that has additional constraints. In addition to being Well Formed, a Valid XML document adheres to the  a set of rules as defined by a Document Type Definition (DTD) or a Schema.  A Parsing Editor, software that understands XML syntax, will check your documents for conformity in terms of the rules of Well-Formedness, and if your document calls a DTD or Schema, the parsing editor will also check that your document abides by  additional constraints, which include element and attribute names, where in the document elements and attributes can be used, and how frequently elements can be used.