Tag Syntax
Superficially, XML tags look like HTML tags. Start-tags begin with
<
and end-tags begin with
</
. Both of these are followed
by the name of the element and are closed by >
. However, unlike HTML tags, you are
allowed to make up new XML tags as you go along. To describe a
person, use <person>
and
</person>
tags. To describe
a calendar, use <calendar>
and </calendar>
tags. The
names of the tags generally reflect the type of content inside the
element, not how that content will be formatted.
Empty elements
There’s also a special syntax for empty elements,
elements that have no content. Such an element can be represented
by a single empty-element tag that begins
with <
but ends with
/>
. For instance, in
XHTML, an XMLized reformulation of standard HTML,
the line-break and horizontal-rule elements are written as
<br
/>
and <hr
/>
instead of <br>
and <hr>
. These are exactly equivalent
to <br></br>
and
<hr></hr>
, however.
Which form you use for empty elements is completely up to you.
However, what you cannot do in XML and XHTML (unlike HTML) is use
only the start-tag—for instance <br>
or <hr>
—without using the matching
end-tag. That would be a well-formedness error.
Case-sensitivity
XML, unlike HTML, is case-sensitive. <Person>
is not the same as
<PERSON>
or <person>
. If you open an element
with a <person>
tag, you
can’t close it with a </PERSON>
tag. You’re free to use
upper- or lowercase or both as you choose. You just have to be
consistent within any one element.
Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.