D.3 How Do I Convert DTDs and RELAX NG to XSD?


Whether you're working with legacy schemas or simply prefer to work in these simpler frameworks, you need to convert these forms to XSD for work in Office. As noted earlier, Trang, available from http://www.thaiopensource.com/relaxng/trang.html, offers an extremely easy path from DTDs or RELAX NG to XSD. At its core, Trang is a simple command-line utility that takes XML sample documents, DTDs, RELAX NG, RELAX NG Compact syntax, or XSD and converts them into DTDs, RELAX NG, RELAX NG Compact syntax, or XSD. For working with Office, you'll mostly be converting DTDs and RELAX NG to XSD, though perhaps you'll want to convert XSD to other forms to use with other systems.

RELAX NG is more expressive than XSD in a number of ways. If you really take advantage of RELAX NG, the limitations of XSD will be fairly apparent, and Trang can't convert all of RELAX NG's capabilities to XSD. If you stick with the subset shown in this appendix, however, you should not encounter such losses.


The basic syntax for using Trang looks like:

java -jar trang.jar sourceFile destinationFile

By default, the kind of transformation Trang performs depends on the file extensions of the source and destination files, shown in Table D-3.

Table D-3. File extensions used by Trang

File extension

Meaning

.xsd

W3C XML Schema (XSD) file (output only)

.dtd

XML 1.0 Document Type Definition (DTD)

.rng

RELAX NG file, XML syntax

.rnc

RELAX NG file, compact syntax

.xml

XML instance file (source only)


Converting the DTD shown in Example D-2 to XSD is as easy as typing:

java -jar trang.jar appD-2.dtd appD-8.xsd

at the command prompt. The resulting XSD file is shown in Example D-8.

Example D-8. The result of converting the DTD in Example D-2 to XSD
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">   <xs:element name="authors">     <xs:complexType>       <xs:sequence>         <xs:element minOccurs="0" maxOccurs="unbounded" ref="person"/>       </xs:sequence>     </xs:complexType>   </xs:element>   <xs:element name="person">     <xs:complexType>       <xs:sequence>         <xs:element ref="name"/>         <xs:element ref="nationality"/>       </xs:sequence>       <xs:attribute name="abbrev" use="required"/>     </xs:complexType>   </xs:element>   <xs:element name="name" type="xs:string"/>   <xs:element name="nationality" type="xs:string"/> </xs:schema>

Next, we'll convert the RELAX NG schema shown in Example D-7 to the XSD shown in Example D-9:

java -jar trang.jar appD-7.rnc appD-9.xsd

The RELAX NG schema in Example D-7 included some features that weren't in the DTD, notably namespaces and datatypes, reflected in the resulting XSD, which now includes a targetNamespace attribute and an xs:token for the abbrev attribute. Trang also prefixes child element names with authors not necessary, but it does make some aspects of the schema clearer if there are multiple namespaces used.

Example D-9. The result of converting the RELAX NG in Example D-7 to XSD
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"  targetNamespace="http://example.com/authors/"  xmlns:authors="http://example.com/authors/">   <xs:element name="authors">     <xs:complexType>       <xs:sequence>         <xs:element minOccurs="0" maxOccurs="unbounded" ref="authors:person"/>       </xs:sequence>     </xs:complexType>   </xs:element>   <xs:element name="person">     <xs:complexType>       <xs:sequence>         <xs:element ref="authors:name"/>         <xs:element ref="authors:nationality"/>       </xs:sequence>       <xs:attributeGroup ref="authors:abbrev"/>     </xs:complexType>   </xs:element>   <xs:attributeGroup name="abbrev">     <xs:attribute name="abbrev" use="required" type="xs:token"/>   </xs:attributeGroup>   <xs:element name="name" type="xs:string"/>   <xs:element name="nationality" type="xs:string"/> </xs:schema>

If you need to create an XSD from a sample document, you can also do that. Running Trang on our sample document to create an XSD from Example D-3 produces the result shown in Example D-10.

Example D-10. The result of converting the XML document in Example D-3 to XSD
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">   <xs:element name="authors">     <xs:complexType>       <xs:sequence>         <xs:element maxOccurs="unbounded" ref="person"/>       </xs:sequence>     </xs:complexType>   </xs:element>   <xs:element name="person">     <xs:complexType>       <xs:sequence>         <xs:element ref="name"/>         <xs:element ref="nationality"/>       </xs:sequence>       <xs:attribute name="abbrev" use="required" type="xs:NCName"/>     </xs:complexType>   </xs:element>   <xs:element name="name" type="xs:string"/>   <xs:element name="nationality" type="xs:NCName"/> </xs:schema>

Example D-10 handles the attribute declaration for abbrev differently from the other transformations, and has less information generally than the ones generated from the DTD and the RELAX NG schema. For quick and dirty work, this kind of transformation may be very useful, though the results are only as good as the sample documents you provide.

For a more sophisticated approach to generating schemas from sample documents, see http://examplotron.org. Examplotron lets you annotate the sample documents to provide additional information used in generating schemas. Examplotron produces RELAX NG, which Trang can then convert to XSD.


Once you've created the XSD files, you can use them in conjunction with Office just like any other XSD.



Office 2003 XML
Office 2003 XML
ISBN: 0596005385
EAN: 2147483647
Year: 2003
Pages: 135

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net