Page 613 - Beginning PHP 5.3
P. 613
Chapter 19: Working with XML
Like HTML, XML documents are composed primarily of elements and attributes. Each element may
optionally contain one or more child elements; in the example document just shown there are two item
elements inside the root stockList element, and each item element itself contains three child elements.
An element may also contain plain text rather than child elements (such as the text apple inside the first
name element in the example document).
It ’ s also possible for an XML element to contain both child elements and plain text, though this usage
isn ’ t that common in practice.
Each element can optionally have one or more attributes; these are specified in the format name= “ value ”
inside the element ’ s opening tag (such as < item type= “ fruit “ > in the example document).
Anyone can write XML documents, and many folks also design applications to handle XML documents
— both reading existing documents and composing new ones. The XML specification is free for anyone
to use; the World Wide Web Consortium at www.w3.org authored and maintains the latest versions of
the spec.
Although you can write XML documents just by creating arbitrary elements and attributes — as shown
in the stockList example earlier — often you want to formally specify the elements and attributes that
are allowed in a document, as well as their meaning and structure. This is so that, when you exchange
data with another person or application, both parties to the transaction know exactly what the element
and attribute names mean. To do this, you use either a document type definition (DTD) or an XML
Schema definition (XSD); DTDs are discussed in detail a little later in this chapter.
Frequently when you create XML documents, you ’ ll either use an existing publicly available DTD (or
XSD) or use one you ’ ve written yourself. Once you write a DTD, you can publish it on the Web. That
means anyone who needs to read or write an XML document compatible with your system has the
capability to access the published DTD to make sure the document is valid.
XML Document Structure
Two terms that you hear frequently when discussing XML are well - formed and valid . A well - formed XML
document follows the basic XML syntax rules (to be discussed in a minute), and a valid document also
follows the rules imposed by a DTD or an XSD. In other words:
❑ All XML documents must be well - formed — A well - formed XML document uses correct XML
syntax. It may contain any elements, attributes, or other constructs allowed by the XML
specification, but there are no rules about what the names of those elements and attributes can
be (other than the basic naming rules, which are really not much of a restriction) or about what
their content can be. It is in this extensibility that XML really derives a lot of its power and
usefulness; so long as you follow the basic rules of the XML specification, there ’ s no limit to
what you can add or change
❑ An XML document can also be valid — A well - formed document does not need to be valid, but
a valid document must be well - formed. If a well - formed document contains a reference to a
DTD or XSD, the document can be checked against the DTD or XSD to determine if it ’ s valid. An
XML document is valid if its elements, attributes, and other contents follow the rules in the DTD
or XSD. Those rules dictate the names of elements or attributes in the document, what data
those elements and attributes are allowed to contain, and so on
575
9/21/09 9:17:43 AM
c19.indd 575 9/21/09 9:17:43 AM
c19.indd 575