Java Reference
In-Depth Information
Generating a Schema from an XML Document
Problem
You have an XML document instance and you want to quickly generate a valid schema for it.
Solution
Use Trang, which is available as a free download from http://www.thaiopensource.com .
After you unpack the download, you can invoke Trang like this:
>java -jar C:/programs/trang/trang.jar C:/repository/src/xml/ch02/
Catalog.xml
Catalog.xsd
This runs the Trang program and passes it two inputs: the name of the XML file you want to
generate a schema for and the name of the output schema file you want Trang to create.
Given your Catalog.xmlfile from Writing XML Data Streams as input, Trang generates the
following schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="book"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:choice>
<xs:element ref="author"/>
<xs:element ref="authors"/>
</xs:choice>
<xs:element ref="price"/>
<xs:element ref="category"/>
</xs:sequence>
<xs:attribute name="sku" use="required" type="xs:NMTOKEN"/>
</xs:complexType>
Search WWH ::




Custom Search