1、俄罗斯套娃式:整个文档只有一个根节点。所有属性及元素进行嵌套。
优点:结构性强,比较容易看。
缺点:类型重用性低。
代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/2"
xmlns:tns="http://www.example.org/2"
elementFormDefault="qualified">
<element name="books">
<complexType>
<sequence minOccurs="1" maxOccurs="unbounded">
<element name="book">
<complexType>
<sequence>
<element name = "title"/>
<element name = "content"/>
<choice>
<element name = "author"></element>
<element name = "authors">
<complexType>
<sequence maxOccurs="unbounded">
<element name = "author" type = "string"/>
</sequence>
</complexType>
</element>
</choice>
</sequence>
<attribute name = "id" type = "int" use = "required" />
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
2、腊肠式:所有元素与类型定义分离。
优点:重用性强。
缺点:不易维护。
代码:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/03"
xmlns:tns="http://www.example.org/03"
elementFormDefault="qualified">
<element name="book" type="tns:bookType"></element>
<element name="id" type="int"/>
<element name="title" type="string"/>
<element name="content" type="string"/>
<complexType name="bookType">
<sequence>
<element ref="tns:id"/>
<element ref="tns:title"/>
<element ref="tns:content"/>
</sequence>
</complexType>
</schema>
3、百叶窗式:集成前面两者的优点。
代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/04"
xmlns:tns="http://www.example.org/04"
elementFormDefault="qualified">
<element name="person" type="tns:personType"/>
<complexType name="personType">
<sequence>
<element name="name" type="string"/>
<element name="age" type="tns:ageType"/>
<element name="email" type="tns:emailType"/>
</sequence>
<attribute name="sex" type="tns:sexType"/>
</complexType>
<simpleType name="emailType">
<restriction base="string">
<pattern value="(\w+\.*)*\w+@\w+\.[A-Za-z]{2,6}"/>
<minLength value="6"/>
<maxLength value="255"/>
</restriction>
</simpleType>
<simpleType name="ageType">
<restriction base="int">
<minInclusive value="1"/>
<maxExclusive value="150"/>
</restriction>
</simpleType>
<simpleType name="sexType">
<restriction base="string">
<enumeration value="男"/>
<enumeration value="女"/>
</restriction>
</simpleType>
</schema>