<!--schema-枚举-->
案例:1
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn" xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">
<!-- 定义一个简单类型 -->
<xs:simpleType name="carType">
<!-- 限定 -->
<xs:restriction base="xs:string">
<!-- 枚举的类型 -->
<xs:enumeration value="宝马" />
<xs:enumeration value="奥迪" />
<xs:enumeration value="捷豹" />
</xs:restriction>
</xs:simpleType>
<!-- 简易类型的 -->
<xs:element name="note">
<!-- 符合类型 -->
<xs:complexType>
<!-- 有顺序的 -->
<xs:sequence>
<xs:element name="to">
<!-- 简单类型 -->
<xs:simpleType>
<!-- 限定 -->
<xs:restriction base="xs:string">
<!-- 只能够出现a-z之间的任意一个字母,这就是正则表达式 -->
<xs:pattern value="[a-zA-Z][A-Z][0-9]"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="from">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z]+)"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="heading">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="body" type="xs:string" />
<xs:element name="xdate" type="xs:date" fixed="2010-12-10" />
<!-- 定义age元素,默认值是12 -->
<xs:element name="age" default="12">
<!-- 定义一个简单类型 -->
<xs:simpleType>
<!-- 限定 -->
<xs:restriction base="xs:integer">
<!-- 最小值 -->
<xs:minInclusive value="0" />
<!-- 最大值 -->
<xs:maxInclusive value="120" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<!-- 定义一个user元素 -->
<xs:element name="user">
<!-- 复合类型 -->
<xs:complexType>
<xs:sequence>
<xs:element name="sex">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="男|女"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<!-- 定义一个属性 -->
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<!-- 定义car元素 -->
<xs:element name="car1" type="carType" />
<!-- 定义car元素 -->
<xs:element name="car2">
<!-- 定义一个简单类型 -->
<xs:simpleType>
<!-- 限定 -->
<xs:restriction base="xs:string">
<!-- 枚举的类型 -->
<xs:enumeration value="宝马" />
<xs:enumeration value="奥迪" />
<xs:enumeration value="捷豹" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<!-- 定义一个元素 -->
<xs:element name="phone">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[1][358][0-9]{9}" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<!--被约束文件-->
<?xml version="1.0" encoding="UTF-8"?>
<note xmlns="http://www.w3school.com.cn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3school.com.cn note.xsd">
<to>cA4</to>
<from>wang</from>
<heading>aZbVrU</heading>
<body></body>
<xdate>2010-12-10</xdate>
<age>120</age>
<user name="">
<sex>男</sex>
</user>
<car1>捷豹</car1>
<car2>宝马</car2>
<!-- 定义一个字符1 第二3 5 8的任意一个 -->
<phone>13521768697</phone>
</note>