Schema的几种设计方案
1.Russian Doll 俄罗斯玩偶
只有一个根元素,通过嵌套的方式完成编写
优点:结构清晰,根元素只有一个
缺点:元素无法重用
books.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/02"
xmlns:tns="http://www.example.org/02"
elementFormDefault="qualified">
<!--
元素的定义(元素及子元素)
属性的定义,在sequence之后
Russian Doll
只有一个根元素,通过嵌套的方式完成编写
优点:结构清晰,根元素只有一个
缺点:元素无法重用
-->
<element name="books">
<complexType>
<!-- sequence: 子元素按照顺序出现 -->
<sequence maxOccurs="unbounded">
<element name="book">
<complexType>
<sequence minOccurs="1" maxOccurs="unbounded">
<element name="title" type="string" />
<element name="content" type="string" />
<!-- choice: 多个子元素中选择一个 -->
<choice>
<element name="author" type="string" />
<element name="authors">
<complexType>
<!-- all: 子元素出现的顺序任意,但每个子元素可出现零次或一次 -->
<all>
<element name="author" type="string" />
</all>
<!-- 如果有多个author,即author子元素出现多次,改用sequence标签
<sequence maxOccurs="3">
<element name="author" type="string" />
</sequence> -->
</complexType>
</element>
</choice>
</sequence>
<!-- book元素的属性,属性的定义,在sequence之后 -->
<attribute name="id" type="int" use="required" />
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
对应的books_1.xml(注意在eclipse中引入对应的xml catalog)
<?xml version="1.0" encoding="UTF-8"?>
<book:books xmlns:book="http://www.example.org/02"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="02.xsd">
<book:book id="1">
<book:title>Java in action</book:title>
<book:content>java is good</book:content>
<book:author>Bruce</book:author>
</book:book>
<book:book id="2">
<book:title>SOA in action</book:title>
<book:content>soa is difficult</book:content>
<book:authors>
<book:author>Jack</book:author>
<book:author>Mike</book:author>
</book:authors>
</book:book>
</book:books>
因为引入books.xsd命名空间的时候加了前缀,xmlns:book=http://www.example.org/02,所以对于books.xsd中元素的引用都需要加入前缀book。
2.Salami Slice 腊肠切片
优点:能够进行最大化的重用
缺点:根元素不清晰
<?xml version="1.0" encoding="UTF-8"?>
<!--
Salami Slice
优点:能够进行最大化的重用
缺点:根元素不清晰
-->
<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 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.Venetian Blind 百页窗
一个根节点
通过simpleType完成重用
<?xml version="1.0" encoding="UTF-8"?>
<!--
Venetian Blind
一个根节点
通过simpleType完成重用
-->
<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" />
<!-- 将sex定义为子元素 -->
<!--<element name="sex" type="tns:sexType" /> -->
<element name="email" type="tns:emailType" />
</sequence>
<!-- 将sex定义为属性 -->
<attribute name="sex" type="tns:sexType" />
</complexType>
<!-- 简单类型,通过对简单类型的描述,完成重用。
(可定义不同的element,type为某个简单类型,完成重用)-->
<simpleType name="ageType">
<restriction base="int">
<minInclusive value="1" />
<maxExclusive value="300" />
</restriction>
</simpleType>
<simpleType name="sexType">
<restriction base="string">
<enumeration value="男" />
<enumeration value="女" />
</restriction>
</simpleType>
<simpleType name="emailType">
<restriction base="string">
<pattern value="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" />
<minLength value="6" />
<maxLength value="255" />
</restriction>
</simpleType>
</schema>
对应的person_1.xml
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns="http://www.example.org/04"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/04"
sex="女">
<name>姚双双</name>
<age>25</age>
<email>yaoshuangg@163.com</email>
</person>
以上是schema的几种设计方案,通常使用第三种方案,即Venetian Blind百页窗式。
综上,元素的定义采用<element>标签;通过<complexType>定义子元素及子元素的顺序、数量等;通过<simpleType>定义多种简单类型,完成重用;属性的定义在<sequence>之后(,<complexType>之中)。