XML is not a replacement for HTML.
XML and HTML were designed with different goals:
- XML was designed to describe data, with focus on what data is
- HTML was designed to display data, with focus on how data looks
HTML is about displaying information, while XML is about carrying information.
An XML element is everything from (including) the element’s start tag to (including) the element’s end tag.
An element can contain:
- other elements
- or a mix of all of the above…
<author>J K. Rowling</author>
<author>Erik T. Ray</author>
In the example above, <bookstore> and <book> have element contents, because they contain other elements. <book> also has an attribute (category=”CHILDREN”). <title>, <author>, <year>, and <price> have text content because they contain text.
XML elements can have attributes, just like HTML.
Attributes provide additional information about an element.
In HTML, attributes provide additional information about elements:
Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but can be important to the software that wants to manipulate the element:
Attribute values must always be quoted. Either single or double quotes can be used. For a person’s gender, the person element can be written like this:
or like this:
If the attribute value itself contains double quotes you can use single quotes, like in this example:
<gangster name=’George “Shotgun” Ziegler’>
or you can use character entities:
<gangster name=”George "Shotgun" Ziegler”>
- XML Elements vs. Attributes
Take a look at these examples:
In the first example gender is an attribute. In the last, gender is an element. Both examples provide the same information.
There are no rules about when to use attributes or when to use elements. Attributes are handy in HTML. In XML my advice is to avoid them. Use elements instead
XML Syntax Rules
All XML Elements Must Have a Closing Tag
XML Tags are Case Sensitive
XML Elements Must be Properly Nested