<!--Schema-仅含有文本-->
案例
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/text"
xmlns:tns="http://www.example.org/text"
elementFormDefault="qualified">
<!-- xmlschema仅含有文本 -->
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="name">
<!-- 简单的类型即可 -->
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]{9}"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
被约束文件
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://www.example.org/text" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/text text.xsd">
<book>
<name>aaaaaaaaa</name>
</book>
</books>
案例2:
<!--混合类型-无序->
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/text"
xmlns:tns="http://www.example.org/text"
elementFormDefault="qualified">
<!-- xmlschema仅含有文本 -->
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book">
<!-- mixed=true代表是混合类型的-->
<xs:complexType mixed="true">
<!--代表无序-->
<xs:all>
<xs:element name="name" maxOccurs="1">
<!-- 简单的类型即可 -->
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]{9}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="price" type="xs:double"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
无序
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://www.example.org/text" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/text text1.xsd">
<book>
<!-- all代表的是无序的 -->
<price>12.9</price>
<name>xxxxxxxxx</name>
</book>
</books>
案例3:
<!--互斥-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/text"
xmlns:tns="http://www.example.org/text"
elementFormDefault="qualified">
<!-- xmlschema仅含有文本 -->
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book">
<!-- mixed=true代表是混合类型的-->
<xs:complexType mixed="true">
<!-- choice代表的是互斥的 -->
<xs:choice>
<xs:element name="name" maxOccurs="1">
<!-- 简单的类型即可 -->
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]{9}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="price" type="xs:double"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
被约束文件
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://www.example.org/text" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/text text3.xsd">
<book>
<!-- choice代表的是互斥的 -->
<price>12.9</price>
</book>
</books>
案例4
<!--schema--->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/text"
xmlns:tns="http://www.example.org/text"
elementFormDefault="qualified">
<!-- xmlschema仅含有文本 -->
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book">
<!-- mixed=true代表是混合类型的-->
<xs:complexType mixed="true">
<!-- sequence代表的是有序的 -->
<xs:sequence>
<xs:element name="name" maxOccurs="5">
<!-- 简单的类型即可 -->
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]{9}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="price" type="xs:double" minOccurs="2" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
被约束文件
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://www.example.org/text" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/text text4.xsd">
<book>
<!-- sequence代表的是有序的 -->
<name>xxxxxxxxx</name>
<name>xxxxxxxxx</name>
<name>xxxxxxxxx</name>
<name>xxxxxxxxx</name>
<name>xxxxxxxxx</name>
<price>12.9</price>
<price>12.9</price>
<price>12.9</price>
<price>12.9</price>
<price>12.9</price>
<price>12.9</price>
<price>12.9</price>
<price>12.9</price>
<price>12.9</price>
<price>12.9</price>
</book>
</books>
案例5
<!--定义组-有序单个->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/text"
xmlns:tns="http://www.example.org/text"
xmlns="http://www.example.org/text"
elementFormDefault="qualified">
<!-- 定义组 -->
<xs:group name="bookGroup">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="price" type="xs:double"/>
<xs:element name="cdata" type="xs:date"/>
</xs:sequence>
</xs:group>
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<!-- 引用组 -->
<xs:group ref="bookGroup"/>
<xs:element name="author" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
被约束文件
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://www.example.org/text" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/text text5.xsd">
<book>
<!-- group代表的是有序的 -->
<name>xxxxxxxxx</name>
<price>12.9</price>
<cdata>2010-10-10</cdata>
<author>chenhj</author>
</book>
</books>
案例6
<!--定义组-无序、多个->
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/text" xmlns:tns="http://www.example.org/text"
xmlns="http://www.example.org/text" elementFormDefault="qualified">
<!-- 定义组 -->
<xs:group name="bookGroup">
<xs:choice>
<xs:element name="name" type="xs:string" />
<xs:element name="price" type="xs:string" />
<xs:element name="cdata" type="xs:string" />
</xs:choice>
</xs:group>
<xs:element name="books">
<xs:complexType>
<xs:all>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<!-- 引用组 -->
<xs:group ref="bookGroup" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
被约束文件
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://www.example.org/text"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/text text6.xsd">
<book>
<!-- group代表的是有序的 -->
<!-- 无序,多次 -->
<name>xxxxxxxxx</name>
<cdata>2</cdata>
<price>2</price>
<price>2</price>
<price>2</price>
<price>2</price>
<price>2</price>
<price>2</price>
<price>2</price>
</book>
</books>