The Simplified Guide to XML Syntax (with Examples)

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Understanding XML, its syntax, its uses, and its constraints!

Article published on 03/09/2024, last updated on 10/08/2026

XML is a generic language for easily representing complex data in a text file; the name "XML" is an acronym for eXtensible Markup Language (or Extensible Markup Language).

It's an open format, created in 1998 by the W3C (World Wide Web Consortium)

Here is a basic example of data written in XML:

<?xml version="1.0"?>
<!DOCTYPE Library SYSTEM "https://example.com/xml/library.dtd">
<Library>
   <Book id="1">
      <Author>Garghentini, Davide</Author>
      <Title>XML Developer's Guide</Title>
   </Book>
</Library>

The syntax

Let's now explore the syntax of a file, so you can understand it, and write your own XML files!

The prolog (the header)

The "prolog" is this tag, right at the very beginning of the file:

<?xml version="1.0"?>

It simply indicates that the document is in XML format, and also specifies the version of the format used.

Note that there is a version 1.1 for XML, but it is recommended to use 1.0, as explained here.

The prolog is actually optional (for most parsers), but if present, it must absolutely appear first in the document.

The Document Type Definition (schema)

A DTD is a document in which is precisely described the data structure to follow for an XML document to be valid in its content:

<!DOCTYPE Library SYSTEM "https://example.com/xml/library.dtd">

If a DTD is declared at the beginning of your XML file, then it must comply with the schema for it to be considered "well-formed" (syntax) and "valid" (content).

We won't dwell on the DTD, since not all XML files need validation.

But if the topic interests you, we've published a complete article on the syntax and workings of DTD files!

Elements

All the content of an XML file is found within what is called a root element, which itself contains a more or less large hierarchy of child elements.

Each element is made up of three parts:

  • The name of the element (mandatory)
  • The attributes (optional)
  • Its content (optional)

Here is an example element:

<text id="1">
	Lorem Ipsum
</text>

We'll say that the text element has an id attribute with the value "1" and its content is text with the value "Lorem Ipsum"

Namespaces

It is sometimes necessary to group elements intended for (or coming from) different applications, which can sometimes cause conflicts at the level of element names.

This is why it is possible to use "namespaces", which are prefixes to precisely identify an element.

An example with HTML elements that we will use with a prefix, thanks to the xmlns attribute, like this:

<root>
	<html:table xmlns:html="https://www.w3.org/TR/html5/">
	  <html:tr>
	    <html:td>Lorem</h:td>
	    <html:td>Ipsum</h:td>
	  </html:tr>
	</html:table>
</root>

The URL used for a namespace generally points to a technical specification, but nothing requires this, as long as the identifier is unique!

Attributes

An attribute is simply a piece of data that concerns the element itself, and whose content must necessarily be in the form of a character string:

unAttribut="uneValeur"

We sometimes speak of key - value data

An example describing a person using attributes:

<Person firstname="John" lastname="Doe"/>

Content

As we saw previously, content can be of 3 different types:

  • Empty
  • Other child elements
  • Raw data (i.e. text)

Which gives us this kind of elements:

<!-- empty -->
<Person firstname="John" lastname="Doe"/>

<!-- children -->
<School>
	<Course/>
	<Course/>
</School>

<!-- Raw data -->
<Text>
	Lorem ipsum
</Text>

For raw data, there are two formats which are CDATA and PCDATA, which we will see right now!

PCDATA vs CDATA

To put it simply, PCDATA (for Parsed Character Data) represents text content that can be interpreted by the XML parser. Example:

<Text>
	Hello <span>World</span>
</Text>

Here, the content will be interpreted, and the content <span>World</span> will be treated as an XML element.

To avoid this, while including content with characters like <, > and /, you can use the CDATA format (for Character Data). You will then need to insert your content within a <![CDATA[…]]> tag like this:

<Text>
	<![CDATA[[Hello <span>World</span>]]>
</Text>

In this case, our data will be considered as plain text, with no risk of code interpretation.

Comments

It is also possible to use a "comment" tag to add notes in our file, for reference or to add context, but which will be ignored when interpreting the XML file:

<!--Your comment-->

A few rules

There are a handful of rules to remember (and apply) so that your XML file is considered valid AND complies with the provided schema (if one exists):

  • An XML document must have only one root element
  • XML elements must always be closed
  • The names of XML tags are case-sensitive
  • The nesting of elements must be respected
  • Attribute values must necessarily be surrounded by quotation marks "

Validating your XML files

Many problems in your applications can arise when your XML file is malformed, has a syntax issue, or even an encoding issue.

This is why it is recommended to use a validator to check that the syntax of your file is correct!

You can freely use the one we developed at Code-Garage: https://apps.code-garage.com/xml-validator

Note that most validators (including this one) validate the overall syntax, but do not validate compliance with a DTD schema.

Good to know

HTML

If you're struck by the resemblance between XML and HTML, that's normal!

HTML was originally a specific grammar of XML (with a set of valid elements like <body>, <p>, <img/> among others), even though in reality today, some documents can be functional HTML pages, but invalid as XML files.

This is why the XHTML standard exists.

SVG

SVG files are actually XML files, in which graphic elements are described using special elements such as <g>, <rect>, <circle>, etc…


Finished reading this article?
Our newsletter

No spam. Only free content, news, and ever more resources to level up your skills!

Join +1500 developers

Comments (0)

to leave a comment

No comments yet