原文转载自 http://blog.163.com/qazwsxzhx@126/blog/static/1844315420123710224597/
(1)只有一个根元素的方式
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/test" xmlns:tns="http://www.example.org/test" elementFormDefault="qualified">
<element name="user">
<complexType>
<sequence>// 可以把sequence改成all,表示id,username,password,birhdate这些可以在xml中无序写。
<element name="id" type="int"></element>
<element name="username" type="string"></element>
<element name="password" type="string"></element>
<element name="birthdate" type="date"></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/NewXMLSchema1"
xmlns:tns="http://www.example.org/NewXMLSchema1" elementFormDefault="qualified">
<element name="users" type="tns:usertype"></element>
<element name="id" type="int"></element>
<element name="username" type="string"></element>
<element name="password" type="string"></element>
<complexType name="usertype">
<sequence minOccurs="1" maxOccurs="unbounded">
<element name="user">
<complexType>
<sequence>
<element ref="tns:id"></element>
<element ref="tns:username"></element>
<element ref="tns:username"></element>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</schema>
多个根节点,但是不清楚哪个是根节点。达到了element代码重用。
(3)上面两种方式的结合。
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/NewXMLSchema1"
xmlns:tns="http://www.example.org/NewXMLSchema1" elementFormDefault="qualified">
<element name="users" type="tns:usertype"></element>
<complexType name="usertype">
<sequence minOccurs="1" maxOccurs="unbounded">
<element name="user">
<complexType>
<sequence>
<element name="id" type="tns:idType"></element>
<element name="username" type="string"></element>
<element name="password" type="string"></element>
<element name="email" type="tns:emailType"></element>
</sequence>
<attribute name="sex" type="tns:sexType" ></attribute>
</complexType>
</element>
</sequence>
</complexType>
<!--限定元素类型-->
<simpleType name="idType">
<restriction base="int">
<minInclusive value="1"></minInclusive>
</restriction>
</simpleType>
<simpleType name="emailType">
<restriction base="string">
<pattern value="(\w+\.*)*\w+@\w+\.[A-Za-z]{2,6}"></pattern>
</restriction>
</simpleType>
<simpleType name="sexType">
<restriction base="string">
<enumeration value="男"></enumeration>
<enumeration value="女"></enumeration>
</restriction>
</simpleType>
</schema>
(4)包含
student.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/NewXMLSchema4"
xmlns:tns="http://www.example.org/NewXMLSchema4" elementFormDefault="qualified">
<element name="student" type="tns:studenttype"></element>
<complexType name="studenttype">
<sequence>
<element name="id" type="tns:idType"></element>
<element name="username" type="string"></element>
<element name="password" type="string"></element>
<element name="email" type="tns:emailType"></element>
</sequence>
<attribute name="sex" type="tns:sexType" ></attribute>
</complexType>
</element>
</sequence>
</complexType>
<simpleType name="idType">
<restriction base="int">
<minInclusive value="1"></minInclusive>
</restriction>
</simpleType>
<simpleType name="emailType">
<restriction base="string">
<pattern value="(\w+\.*)*\w+@\w+\.[A-Za-z]{2,6}"></pattern>
</restriction>
</simpleType>
<simpleType name="sexType">
<restriction base="string">
<enumeration value="男"></enumeration>
<enumeration value="女"></enumeration>
</restriction>
</simpleType>
</schema>
class.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/NewXMLSchema2"
xmlns:tns="http://www.example.org/NewXMLSchema2" elementFormDefault="qualified">
<include schemaLocation="student.xsd"></include>
<element name="classroom" type="tns:classroom"></element>
<complexType name="classroom">
<sequence>
<element name="grade" type="tns:gradeType"></element>
<element name="student" type="tns:studenttype"></element>
</sequence>
</complexType>
<simpleType name="gradeType">
<restriction base="int">
<minInclusive value="2001"></minInclusive>
<maxExclusive value="3000"></maxExclusive>
</restriction>
</simpleType>
</schema>