Summary: This article introduces the 'eXtensible Markup Language' (XML) and tells of it's uses.
What is XML?
XML is a markup language similar to HTML. It stands for eXtensible Markup Language and is a W3C recommended specification as a general purpose markup language. This means, unlike other markup languages, XML is not predefined so you must define your own tags. The primary purpose of the language is the sharing of data across different systems, such as the Internet.
"Correct" XML (Valid and Well-formed)
For an XML document to be correct it must be a well-formed document, conforming to all of XML's syntax rules, and valid, conforming to a specific language's rules.
Most browsers, including Mozilla, offer a debugger that can identify poorly-formed XML documents.
An example of a document that is not well formed is one with an element that has an opening tag with no closing tag and is not self-closing. In the below example this is exactly what is wrong.
<message> <warning> Hello World </message>
A correct example is shown below and is considered well formed.
<message> <warning> Hello World </warning> </message>
To be valid, a XML document needs to conform to some semantic rules which are usually set in a XML Schema or an Document Type Definition. An example of a document that is not valid is a document that contains an undefined tag. For example if we never defined the warning tag then our document would not be valid.
Like HTML, XML offers methods (called entities) for referring to some special reserved characters (such as a greater then sign which is used for tags). There are five of these characters that you should know:
< < A less than sign. > > A greater than sign. & & Ampersand (And Sign). " " A single quotation mark. ' ' A single apostrophe.
Eventhough there are only 5 declared entities, more can be added using the document's Document Type Definition.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE body [ <!ENTITY warning "Warning: Something bad happened... please refresh and try again."> ]> <body> <message> &warning; </message> </body>
You can also use numeric character references to use special characters such as © for a © sign. More information on this can be found in the Numeric character reference article.
XML is usually used for description but there are ways to display XML data. Without defining a way for XML to display it's self, raw XML code is shown in the browser.
One way to have a style on an XML page is to use CSS with the
<?xml-stylesheet type="text/css" href="stylesheet.css"?>
There is also another more powerful way to display XML, this is using the Extensible Stylesheet Language (XSL) which can be used to transform XML into other languages such as HTML. This makes XML incredibly versatile.
<?xml-stylesheet type="text/xsl" href="transform.xsl"?>
This article is only a quick introduction to what XML is, with a few small examples and references to get you started and interested in XML. For more details about XML, you should look around on the Web for more in-depth articles.
Learning the HTML markup language will help you better understand XML. You should also explore the Mozilla Developer Center for more information.
A list of references on the web have been provided below:
- W3Schools: DTD Tutorial
- W3Schools: XML Tutorial
- Extensible Markup Language (XML) @ W3.org
- XML Example @ A List Apart
- Using XML @ A List Apart
The Using XML article is a great resource on information for transforming and creating your own language.
- Author(s): Justin G. Shreve
- Last Updated Date: August 4th 2007