Java Reference
In-Depth Information
<element name= "library" type= "tns:LibraryType" />
<complexType name= "LibraryType" >
<sequence>
<element ref= "tns:book" maxOccurs= "unbounded" />
</sequence>
</complexType>
<element name= "book" >
<complexType>
<sequence>
<element name= "title" type= "string" />
<element name= "author" type= "string"
maxOccurs= "unbounded" />
<element name= "price" type= "tns:PriceType" />
</sequence>
<attribute name= "isbn" type= "tns:ISBNtype" />
</complexType>
</element>
<simpleType name= "PriceType" >
<restriction base= "decimal" >
<fractionDigits value= "2" />
</restriction>
</simpleType>
<simpleType name= "ISBNtype" >
<restriction base= "string" >
<pattern value= "\d{10}|\d{13}" />
</restriction>
</simpleType>
</schema>
This is the same as the DTD, except that it says that price elements have two decimal
places, and isbn attributes are composed ofeither 10or13decimal digits. Tying the XML
document to this schema can be done by modifying the root element as follows:
<library
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns= "http://www.kousenit.com/books"
xsi:schemaLocation= "
http://www.kousenit.com/books
books.xsd" >
The rest of the library is the same as before. Here's the code used to validate the XML
document against the schema:
String file = "b ooks.xml"
String xsd = " books.xsd"
SchemaFactory factory = SchemaFactory. newInstance (
XMLConstants. W3C_XML_SCHEMA_NS_URI )
Schema schema = factory.newSchema( new File(xsd))
Search WWH ::




Custom Search