Notice a few facts about the preceding XML code :
Because elements can contain other elements, to a certain extent attributes and sub-elements are interchangeable. For instance, in the person element I described the person's name with an lname and an fname attribute. Instead, I could have had each person element contain an lname and a fname subelement, each of which had the appropriate name between the begin and end tag.
In other words:
<person>
<lname>Smith</lname>
<fname>John</fname>
<info name="workphone">800-555-1212</info>
<info name="homephone">407-555-5555</info>
<info name="relationship">Skating buddy</info>
<info name="skatetype">Racing inlines</info>
</person>
Please remember there are no reserved words in the preceding example. info and name are just strings I decided upon to make it self documenting. As an alternative to the preceding, I could have even used info tags to accomplish the same purpose:
<person>
<info name="lname">Smith</info>
<info name="fname">John</info>
<info name="workphone">800-555-1212</info>
<info name="homephone">407-555-5555</info>
<info name="relationship">Skating buddy</info>
<info name="skatetype">Racing inlines</info>
</person>
Your choice of attributes vs. elements depends on things such as whether you'll need more than one of the entity (no two attributes of a single element can have the same name), and whether you should always have the entity (that might favor using an attribute). Also, use elements if order is important, because the XML specification doesn't specify the order of attributes, so parsers don't necessarily preserve attribute order.
The preceding examples have used XML as a hierarchical representation. But it can also be used as stylized markup:
<heading level="3"> Why XML is So
Great</heading>
<paragraph>XML is <emphasis>absolutely wonderful!</emphasis>And
it's not just because <emphasis>XML is <newword>Corporationally
Correct</newword>!</emphasis></paragraph>
<paragraph>Now let's talk about...
In the preceding, the XML markup describes the styles, or functionality, of marked up text. It's up to the application rendering the XML to assign an appearance to such styles. Even the relationship between style and appearance can be moved out of the application using XSL (Extensible Style Language).
Tags must be nested, never interlaced. The following is not allowed:
XML <emph>is <italic>great</emph> and good.</italic>
The well formed way to write the preceding would be to nest tags, like this:
XML <emph>is <italic>great</italic></emph><italic> and good.</italic>
Because tags can't be interlaced, but instead must be nested, all XML represents a hierarchy. For instance, the preceding snippet could be thought of like this:
XML is
<emphasis>
truly
<italic>
great
<italic>
and fantastic.
Generally speaking, in XML intended to represent a hierarchy, an element containing a text node contains no other elements or text nodes, but in XML intended to represent markup, an element often contains several text nodes and several other elements. But this is not a rule, only a custom.