Database Reference
In-Depth Information
Note
Well-formed XML is an XML document that meets a set of constraints specified by the W3C
recommendation for XML 1.0. For example, well-formed XML must contain a root-level element, and any other
nested elements must open and close properly without intermixing.
SQL Server 2005 validates some of the well-formedness constraints. Some rules such as the requirement for a
root-level element are not enforced. For a complete list of well-formedness requirements, refer to the W3C
Understanding XML Documents
An XML document could be a physical file on a computer, a data stream over a network (in theory,
formatted so a human could read it, but in practice, often in compressed binary form), or just a string in
memory. It has to be complete in itself, however, and even without a schema, it must obey certain rules.
The most fundamental rule is that XML documents must be
well-formed
. At its simplest, this means
that overlapping elements aren't allowed, so you must close all
child
elements before the end tag of their
parent
element. For example, this XML document is well-formed:
<states>
<state>
<name>Delaware</name>
<city>Dover</city>
<city>Wilmington</city>
</state>
</states>
It has a
root
(or
document
) element,
states
, delimited by a start tag,
<states>
, and an end tag,
</states>
. The root element is the parent of the
state
element, which is in turn the parent of a
name
element and two
city
elements. An XML document can have only one root element.
Elements may have
attributes
. For example, the previous document could be rewritten as follows,
where
name
is used as an attribute with the
state
element:
<states>
<state name="Delaware">
<city>Dover</city>
<city>Wilmington</city>
</state>
</states>
It retains the same information, replacing the
name
element that occurs only once with a
name
attribute and changing the
content
of the original element (
Delaware
) into the
value
of the attribute
(
"Delaware"
). An element may have any number of attributes, but it may not have duplicate attributes,
so the
city
elements weren't candidates for replacement.
Elements may have content (text data or other elements), or they may be
empty
. For example, if you
want (just for the sake of argument) to keep track of how many states are in the document, you could use
an empty element to do it: