Page 615 - Beginning PHP 5.3
P. 615
Chapter 19: Working with XML
The root element contains two item child elements. Each child element itself contains four children. The
description elements contain CDATA sections to enclose their text data, because the data contains
characters such as > , < , and & that would otherwise be treated as markup:
< item type=”fruit” >
< name > apple < /name >
< unitPrice > 0.99 < /unitPrice >
< quantity > 412 < /quantity >
< description > < ![CDATA[Apples are > > > yummy < < < ]] > < /description >
< /item >
< item type=”vegetable” >
< name > beetroot < /name >
< unitPrice > 1.39 < /unitPrice >
< quantity > 67 < /quantity >
< description > < ![CDATA[Beetroots are lovely & purple]] > < /description >
< /item >
Finally, the root element is closed:
< /stockList >
XML Syntax Rules
You now know what the major components of an XML document are. In addition, a well - formed XML
document must follow all the other syntax rules of the XML specification, the most common of which are
listed here:
❑ XML elements are declared to be either non - empty, in which case they are designed to contain
data; or empty, in which case they cannot contain data. For example, in XHTML, the p
(paragraph) element is non - empty because it can contain text, whereas the br (line - break)
element is empty because it cannot contain anything
❑ Non - empty elements can be created from start and end tags (like the < p > ... < /p > tags in
XHTML). Empty elements should be created using the special empty - element tag format (like
the < br/ > tag in XHTML). Unlike HTML, you cannot have a start tag that isn ’ t followed
by an end tag
❑ XML attributes are written inside the start tags of non - empty elements, or inside the empty -
element tags of empty elements, and must be of the format name= “ value ” or name=’value’ .
No attribute name may appear more than once inside any given element. For example:
< item type=”vegetable” > ... < /item >
< emptyElement color=’red’ / >
❑ XML elements must be properly nested, meaning any given element ’ s start and end tags must
be outside the start and end tags of elements inside it, and inside the start and end tags of its
enclosing element. Here ’ s an example:
< !-- Incorrect nesting -- >
< parent > < child > < /parent > < /child >
< !-- Correct nesting -- >
< parent > < child > < /child > < /parent >
577
9/21/09 9:17:44 AM
c19.indd 577
c19.indd 577 9/21/09 9:17:44 AM