XML stands for Extensible Markup Language, a language that focuses on describing data and what that data actually is. HTML is also a markup language, but it deals with how data looks and is displayed. XML tags aren’t predefined like HTML tags; you invent your own. XML doesn’t do anything by itself. Instead, it helps to structure, store and send information between different systems in a simple, consistent way.
Understanding the Syntax Rules of XML
The first line of an XML document, called the XML declaration, is optional. It gives the version of XML being used (1.0 or 1.1, although 1.0 is by far the most common), as well as the character encoding.
<?xml version="1.0" encoding="UTF-8"?>The example above declares version 1.0 of XML and the UTF-8 character encoding, which is the standard choice today.
The rest of an XML document is made up of nested elements. Each element has a start tag and an end tag. The start tag is formed by putting a name in angle brackets. The end tag uses the same name, with a slash directly after the first angle bracket.
Start tag: <rule>
End tag: </rule><rule>Everything between the start and end tags is called the content.</rule>Everything between the <rule> and </rule> tags in the example above is the content. A complete element has a start tag, content and an end tag, just like the example.
Besides text content, an XML element may also include attributes. An attribute is a name and a value paired together, placed in the start tag directly after the element name.
<term number="1" type="technical">Attribute</term>In the example above, the element named term has 2 attributes: number="1" is one attribute, and type="technical" is another. Both are placed in the start tag, right after the element name (term). In the number="1" attribute, the name number has the value 1. In the type="technical" attribute, the name type has the value technical.
Keep in mind that in XML, attribute values are just text. Although 1 looks like a number, XML treats it as a label describing the data, not as a quantity.
The values of attributes must be put in either single or double quotes. In the example above, the values "1" and "technical" are correctly placed in quotes. Each attribute name may only be used once in any given element.
Elements can include other elements inside of them.
<termlist>
<term>Element</term>
<term>Attribute</term>
<term>Name</term>
<term>Value</term>
</termlist>In this example, the element termlist contains four term elements. The termlist element is known as the root element, or document element. Every XML document must have exactly one root element; XML without one is not well-formed.
Incorrect XML example:
<term>Element</term>
<term>Attribute</term>
<term>Name</term>
<term>Value</term>Without the root element termlist, these term elements are not well-formed XML.
More Differences Between XML and HTML
XML is different than HTML in many subtle but crucial ways, so it follows that there are some tasks that are better suited for XML than HTML, and vice versa – for instance, with XML it is a much simpler task to access crucial document information than with HTML, which would sometimes require an excess of so-called markup language red tape.
HTML doesn’t have to have a closing tag, but XML does (except in the case of the XML declaration, which is not considered an element, so the usual rules don’t apply).
In HTML, some closing tags can be left out. For example, this is valid HTML:
<p>A paragraph in HTML doesn’t have to have a closing tag
<p>New paragraphs can start without the old one being closed.This is incorrect XML, however. In XML, every element must be closed:
<p>A paragraph in XML has a closing tag</p>
<p>Without a closing tag, the XML is not well-formed</p>HTML isn’t case sensitive, but XML is.
<Rule>The capitalization of ‘rule’ is inconsistent here, but fine for HTML</rule>This type of mixed capitalization works in HTML, but it is incorrect XML. In XML, the start and end tags must match exactly:
<rule>The capitalization of ‘rule’ is the same in both tags</rule>Browsers will often accept HTML tags that are nested improperly, but XML tags must be nested properly, closing in the reverse order they were opened, without overlapping.
<b><i>Go to the store, Jimmy!</b></i>Browsers will usually display this, but the <i> tags overlap with the <b> tags, so as XML, the markup above fails. This is the correct version:
<b><i>Go to the store, Jimmy!</i></b>HTML collapses extra white space in a document into a single space, whereas XML preserves all white space.
Original text: Don’t use HTML to preserve space
HTML displays: Don’t use HTML to preserve space
XML keeps: Don’t use HTML to preserve spaceIn the example above, extra spaces were intentionally included. HTML collapses them, while XML keeps them.
With these basics of XML syntax and its differences from HTML under your belt, you should have a firm idea of how to create and use basic, valid XML.