XML Schema design approaches
There are two different approaches for designing an XML Schema:
- Using anonymous complexTypes
- Using named complexTypes
My question is: which one is better?
I find the second one better because it avoids deep nesting and allows reuse of existing types. It is closer to best practices of Software Engineering. But I saw this page, an I am confused now:
Here are some DON'Ts.
- DO NOT try to be a master of XML Schema. It would take months.
- DO NOT use complex types, attribute declarations, and notations.
- DO NOT use local declarations.
- DO NOT use substitution groups.
- DO NOT use schema without the targetNamespace attribute (AKA chameleon schema.)
The fact is, you don't lose anything by following these DON'Ts, as the rest of this paper demonstrates.
There are some explanations, but I don't find them good. But probably I am missing something...
To be precise I'll give you examples of this two approaches. Examples taken from W3Schools page. It lists three methods, but I ignored the second since it is very similar to the first.
Using anonymous complexTypes:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
Using named complexTypes:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringtype">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="inttype">
<xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>
<xs:simpleType name="dectype">
<xs:restriction base="xs:decimal"/>
</xs:simpleType>
<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="shiptotype">
<xs:sequence>
<xs:element name="name" type="stringtype"/>
<xs:element name="address" type="stringtype"/>
<xs:element name="city" type="stringtype"/>
<xs:element name="country" type="stringtype"/>
</xs:sequence>
</xs:complexType>
<xs:complexType 开发者_高级运维name="itemtype">
<xs:sequence>
<xs:element name="title" type="stringtype"/>
<xs:element name="note" type="stringtype" minOccurs="0"/>
<xs:element name="quantity" type="inttype"/>
<xs:element name="price" type="dectype"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="shipordertype">
<xs:sequence>
<xs:element name="orderperson" type="stringtype"/>
<xs:element name="shipto" type="shiptotype"/>
<xs:element name="item" maxOccurs="unbounded" type="itemtype"/>
</xs:sequence>
<xs:attribute name="orderid" type="orderidtype" use="required"/>
</xs:complexType>
<xs:element name="shiporder" type="shipordertype"/>
</xs:schema>
Thank you!
But I saw this page, and I am confused now...
Don't let that page influence your decision. It was written almost ten years ago (Feb 2002, according to Wayback Machine) when XML Schema was new, scary looking and poorly supported. I've worked briefly with the author and know him to be very sharp and pragmatic; however, I think the doc was directed at people who found XML Schema to be scary looking and poorly supported and gave them a cheat sheet for using XML Schema with minimal investment.
A complex type that has a name is more reusable than one that does not. If you want to maximize reusability at the expense of a small amount of typing, name all your complex types. It's your choice.
Kawaguchi's design guidelines say nothing about how the schema is intended to be used. If you want to use a schema just for validation, that's one thing; if you want to use it for data binding in C# or Java, that's another; if you want to use it for writing schema-aware XSLT and XPath, that's another set of considerations again. Frankly, I find his advice a little superficial.
XML Schemas fulfill a variety of possible needs, like data-binding, type-safe processing, validation or interface definition. Your design approach for the schema should reflect your priorities.
For example, having named types is important for advanced use, like if you want to exploit schema-aware XML processing in XQuery or XSLT, but less important if you need the schema just for validation.
精彩评论