Java Reference
In-Depth Information
<person> elements here...
</people>
// <person>
</people>
Define on the root element the xmlns (namespace) with xsi prefix to map to the XML
Schema Instance namespace.
The default xmlns maps to my namespace ( in the schema file in Example 20-12 ) .
De fine the schema location for my namespace to be the file people.xsd ; it is more com-
mon to use an http: URL, but a physical file is acceptable if not scaleable.
Then you need to enable the parser for validation; this is discussed in Parsing XML with
DOM . Any elements in the document not valid according to the schema will result in an
exception being thrown.
XML Document Type Definition
This is not the place for a full dissertation on Document Type Definition syntax. Briefly,
a DTD is a means of restricting the structure of an XML document by listing all the ele-
ments allowed, where they are permitted, and what attributes they have, if any. The DTD
uses a special syntax inherited from SGML. Example 20-13 is people.dtd , a DTD for the
people.xml file shown earlier in this chapter.
Example 20-13. people.dtd
<!ELEMENT people (person)*>
<!ELEMENT person (name, email, country)>
<!ELEMENT name (#PCDATA)>
<!ATTLIST email type CDATA #IMPLIED>
<!ELEMENT email (#PCDATA)>
<!ELEMENT country (#PCDATA)>
To assert that a file conforms to a DTD—that is, to validate the file—you need to refer to
the DTD from within the XML file, as is sometimes seen in HTML documents. The
<!DOCTYPE> line should follow the XML PI line if present but precede any actual data:
<!DOCTYPE people SYSTEM "people.dtd">
<people>
<person>
<name>Ian Darwin</name>
<email>someone@someplace.dom</email>
 
 
 
Search WWH ::




Custom Search