Java Reference
In-Depth Information
Verifying Structure with Schema or DTD
Problem
Up to now, I have simply provided XML and asserted that it is valid. Now you want to verify
the structure using an XML Schema or, rarely, the older Document Type Definition (DTD).
Solution
Write or locate the Schema or DTD and refer to it in one or more XML documents.
Discussion
XML Schema
Due to some limitations of Document Type Definitions, the XML community has been mov-
ing to use of Schema. Although it takes a few seconds longer to write a Schema, there are
tools that will help generate them in most IDEs, and there are tools that can generate a
Schema from existing Java objects (we saw schemagen in Converting Between Objects and
XML with JAXB ) . A schema describes the type system for an open-ended series of valid
XML document instances. Example 20-11 is one possible schema for the people.xml file.
This was generated by XMLSpy .
Example 20-11. people.xsd
<xs:schema
<xs:schema xmlns:xs= "http://www.w3.org/2001/XMLSchema"
targetNamespace= "urn:darwinsys:people"
xmlns:tns= "urn:darwinsys:people"
elementFormDefault="qualified">
<xs:element
<xs:element name= "country" >
<xs:simpleType>
<xs:simpleType>
<xs:restriction
<xs:restriction base= "xs:string" >
<xs:enumeration
<xs:enumeration value= "Canada" //>
<xs:enumeration
<xs:enumeration value= "USA" //>
</xs:restriction>
</xs:restriction>
</xs:simpleType>
</xs:simpleType>
</xs:element>
</xs:element>
<xs:element
<xs:element name= "email" >
<xs:complexType
<xs:complexType mixed= "true" >
<xs:attribute
<xs:attribute name= "type" >
 
 
 
Search WWH ::




Custom Search