例1:
Schema文档:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="book_info" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="book_info">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="author" maxOccurs="3">
<xs:simpleType>
<xs:list itemType="author_3"/>
</xs:simpleType>
</xs:element>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>
<xs:simpleType name="author_3">
<xs:restriction base="xs:string">
<xs:enumeration value="张三"/>
<xs:enumeration value="李四"/>
<xs:enumeration value="赵五"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="019.xsd">
<book id="001">
<name>XML学习天下</name>
<author>张三</author>
<author>李四</author>
<price>88.50</price>
</book>
<book id="002">
<name>XML百例</name>
<author>赵五</author>
<author>李四</author>
<price>68.00</price>
</book>
</books>
注:书的作者只能是张三、李四和赵五。
例2:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded" type="book_value"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="book_value">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="author" type="author_3" maxOccurs="3"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>
<xs:simpleType name="author_3">
<xs:restriction base="xs:string">
<xs:enumeration value="张三"/>
<xs:enumeration value="李四"/>
<xs:enumeration value="赵五"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="018.xsd">
<book id="001">
<name>XML学习天下</name>
<author>张三</author>
<price>88.50</price>
</book>
<book id="002">
<name>XML百例</name>
<author>赵五</author>
<price>68.00</price>
</book>
</books>
注:书的作者只能是张三、李四、赵五其中的一个。
Schema文档:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="book_info" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="book_info">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="author" maxOccurs="3">
<xs:simpleType>
<xs:list itemType="author_3"/>
</xs:simpleType>
</xs:element>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>
<xs:simpleType name="author_3">
<xs:restriction base="xs:string">
<xs:enumeration value="张三"/>
<xs:enumeration value="李四"/>
<xs:enumeration value="赵五"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="019.xsd">
<book id="001">
<name>XML学习天下</name>
<author>张三</author>
<author>李四</author>
<price>88.50</price>
</book>
<book id="002">
<name>XML百例</name>
<author>赵五</author>
<author>李四</author>
<price>68.00</price>
</book>
</books>
注:书的作者只能是张三、李四和赵五。
例2:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded" type="book_value"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="book_value">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="author" type="author_3" maxOccurs="3"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>
<xs:simpleType name="author_3">
<xs:restriction base="xs:string">
<xs:enumeration value="张三"/>
<xs:enumeration value="李四"/>
<xs:enumeration value="赵五"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="018.xsd">
<book id="001">
<name>XML学习天下</name>
<author>张三</author>
<price>88.50</price>
</book>
<book id="002">
<name>XML百例</name>
<author>赵五</author>
<price>68.00</price>
</book>
</books>
注:书的作者只能是张三、李四、赵五其中的一个。