XML declaration and rules of declaration

The declaration's rules are few and rigid: lowercase <?xml, absolutely first (no space, no comment, no blank line above), version compulsory, encoding and standalone optional and in that order, values quoted.

9 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

ગરબા નાઈટ turns to garbage

The committee wants event names bilingual: <name>ગરબા નાઈટ</name> joins events.xml. On your machine, fine. On the vendor's system, the name renders as question marks and boxes.

The file never said what character encoding it uses, so the vendor's parser guessed, wrongly.

The cure is one line you have been typing on autopilot since lesson 1. Today that line gets read closely, because its rules are rigid and exams check every one of them.

Theory

The declaration, member by member

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

  • version: COMPULSORY, always first; 1.0 in practice
  • encoding: optional; names the character set. UTF-8 (the default) covers every script events.xml will meet, ગરબા included. Declaring it removes the vendor's guesswork
  • standalone: optional; yes means the document needs no external DTD file to be understood; no means it might

Delimiters: opens <?xml, closes ?>. It is an instruction, not an element: no closing tag exists, and it never counts as a root.

Theory

The rules of declaration

Five rules, all enforced:

  • Position: if present, absolutely first: no blank line, no space, no comment, not one character above or before it
  • Case: lowercase <?xml; <?XML and <?Xml are illegal
  • version is mandatory and stands first among the attributes
  • Order is fixed: version, then encoding, then standalone; rearranging is an error
  • Values are quoted, single or double, like every XML attribute

And the meta-rule from earlier lessons: the declaration itself is OPTIONAL. No declaration is legal; a misplaced or misspelt one is not.

Practical

One valid, four invalid: spot why

<!-- VALID -->
<?xml version="1.0" encoding="UTF-8"?>

<!-- INVALID: uppercase XML -->
<?XML version="1.0"?>

<!-- INVALID: version missing -->
<?xml encoding="UTF-8"?>

<!-- INVALID: order swapped -->
<?xml encoding="UTF-8" version="1.0"?>

<!-- INVALID: unquoted value -->
<?xml version=1.0?>

Quiz

A file begins with one blank line, then <?xml version="1.0"?> on line 2, then a perfect root element. What does the parser say?

  1. Fine: whitespace before the declaration is ignored
  2. Error: the declaration must be the absolute first thing, and the blank line breaks that
  3. Fine, but the declaration is treated as a comment
  4. Error: version 1.0 is outdated
Show the answer

Error: the declaration must be the absolute first thing, and the blank line breaks that

The position rule is absolute: not even invisible whitespace may precede the declaration, so one innocent blank line invalidates the document: the single most common hand-editing bug in this unit, because editors add leading lines silently. Option A imports HTML's tolerance into a language that has none. Option C invents a downgrade mercy parsers do not perform. Option D is noise: 1.0 remains the standard version in practice. Fix: delete the blank line; the declaration touches the file's first byte.

Think first

Which line saved ગરબા નાઈટ?

Explain precisely how encoding="UTF-8" fixes the vendor's question-mark garbage, before tapping. What was the vendor's parser doing without it?

Show the answer

Text in a file is just bytes; an encoding is the codebook mapping bytes back to characters. Gujarati letters in UTF-8 span multiple bytes each, and the vendor's parser, told nothing, guessed a single-byte codebook: each Gujarati byte decoded as a wrong or unprintable Latin-era character: hence boxes and question marks. Declaring encoding="UTF-8" hands every consumer the right codebook up front. Habit: any XML that could ever carry non-English text declares UTF-8 explicitly, even though it is the default, because the day it travels is the day defaults differ.

Watch out

Three declaration slips

The invisible blank line: the quiz bug; check the file's very first character when a valid-looking declaration errors.

Treating it as an element: writing </xml> to "close" it, or counting it as a second root: it is an instruction; ?> is its entire ending.

Casual attribute order: encoding before version compiles in no parser; the order version, encoding, standalone is grammar, not style.

Theory

Unit 1 closes: events.xml is complete

Take stock: FestConnect's data lives in one well-formed events.xml: declared UTF-8, one root, quoted attributes, matched cases, clean nesting, sections you can name. You can write XML and, just as valuably, DEBUG a stranger's. Unit 2 changes seats entirely: from data files to the live page, where jQuery makes FestConnect's schedule fold, fade and react. The XML returns in Unit 4, when AJAX fetches it without a reload.

Summary

Key takeaways

  • Form: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>, an instruction, not an element.
  • If present, it is the file's absolute first thing: no space, line or comment above.
  • Lowercase <?xml only; version is compulsory and first; order fixed: version, encoding, standalone; values quoted.
  • encoding names the byte-to-character codebook; UTF-8 covers Gujarati and Hindi safely.
  • standalone="yes" means no external DTD is needed.
  • The declaration is optional; a wrong one is fatal, a missing one is fine.
  • Memory hook: first byte, lowercase, version-encoding-standalone.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction of XML

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

XML declaration and rules of declaration · Web Designing-2 (option A) · Gri-Learn