如何写一个最简单的XML Schema文档呢?首先,我们写出一个最简单的XML文档。
hello.xml
1 2 |
<?xml version="1.0"?> <greeting>Hello World!!</greeting> |
hello.xsd
1 2 3 4 |
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="greeting" type="xsd:string"/> </xsd:schema> |
2.含子元素的稍复杂Schema文档
假设实例文档是如下的:
customer.xml
1 2 3 4 |
<customer> <name>teiki</name> <address>No.237, Road Waitan, Shanghai</address> </customer> |
customer.xsd
01 02 03 04 05 06 07 08 09 10 11 |
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="customer"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="address" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> |
3.含子元素和孙元素的更复杂Schema文档
这次我们给出一个更加复杂一些的文档:
address.xml
1 2 3 4 5 6 7 8 9 |
<customer> <name>Teiki</name> <address> <!-- address追加一个地址子元素 --> <prefecture>Zhejiang</prefecture> <city>Hangzhou</city> <street>Xilu Road, No.121, 7F</street> </address> </customer> |
address.xsd
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="customer"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" type="xsd:string" /> <!-- 追加子元素address--> <xsd:element name="address"> <xsd:complexType> <xsd:sequence> <xsd:element name="prefecture" type="xsd:string" /> <xsd:element name="city" type="xsd:string" /> <xsd:element name="street" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element> <!-- end --> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> |
address2.xsd
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="customer"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" type="xsd:string" /> <xsd:element ref="address" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="address"> <xsd:complexType> <xsd:sequence> <xsd:element name="prefecture" type="xsd:string" /> <xsd:element name="city" type="xsd:string" /> <xsd:element name="street" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> |
4.如何定义属性呢?
加入实例文档的元素包含属性,怎么办呢?
customer2.xml
1 2 3 4 |
<customer id="001718"> <name>Teiki</name> <address>No.237, Road Waitan, Shanghai</address> </customer> |
customer2.xsd
01 02 03 04 05 06 07 08 09 10 11 12 13 |
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="customer"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" type="xsd:string" /> <xsd:element name="address" type="xsd:string" /> </xsd:sequence> <!-- 增加属性定义 --> <xsd:attribute name="id" type="xsd:string" /> </xsd:complexType> </xsd:element> </xsd:schema> |
5.如何定义相同子元素的数量?
先看这个简单的订购数据实例文档:
order.xml
1 2 3 4 |
<order> <orderItem>Accounting Book</orderItem> <orderItem>Taxation Book</orderItem> </order> |
order.xsd
01 02 03 04 05 06 07 08 09 10 |
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="order"> <xsd:complexType> <xsd:sequence> <xsd:element name="orderItem" type="xsd:string" maxOccurs="10" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> |
1 | <xsd:element name="orderItem" type="xsd:string" minOccurs="5" maxOccurs="10"/> |
6.如何定义可选项的子元素?
假如上面的订书数据中,可以用书名或者书号任一一种订购,则实例文档可能如下:
order2.xml
01 02 03 04 05 06 07 08 09 10 |
<order> <orderItem> <!--书名订购--> <name>Accounting Book</name> </orderItem> <orderItem> <!--书号订购--> <id>7-5058-3496-7</id> </orderItem> </order> |
order2.xsd
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="order"> <xsd:complexType> <xsd:sequence> <xsd:element ref="orderItem" maxOccurs="10" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="orderItem"> <xsd:complexType> <xsd:choice> <xsd:element name="name" type="xsd:string" /> <xsd:element name="id" type="xsd:string" /> </xsd:choice> </xsd:complexType> </xsd:element> </xsd:schema> |
再稍微修改一下订书数据的实例文档:
order3.xml
1 2 3 4 5 6 7 8 9 |
<order> <orderItem> <name>Accounting Book</name> <quantity>2</quantity> </orderItem> <orderItem> <id>7-5058-3496-7</id> </orderItem> </order> |
order3.xsd
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="order"> <xsd:complexType> <xsd:sequence> <xsd:element ref="orderItem" maxOccurs="10"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="orderItem"> <xsd:complexType> <xsd:sequence> <xsd:choice> <xsd:element name="name" type="xsd:string"/> <xsd:element name="id" type="xsd:string"/> </xsd:choice> <xsd:element name="quantity" type="xsd:string" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> |
8.内置简单类型
内建于XML Schema的简单类型有44种。他们在XML Schema推荐标准的第二部分中公布,下面这是一张内置类型的层次结构图:
9.自定义简单类型
如果内置简单类型的44种还不能满足要求,怎么办呢?下面学习自定义简单类型。(XML的扩展性充分体现在这里)例如这个实例文档:
order4.xml
1 2 3 4 5 6 |
<order> <orderItem> <id>7-5058-3496-7</id> <quantity>5</quantity> </orderItem> </order> |
1 2 3 4 5 |
<xsd:simpleType name="idType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{1}-\d{4}-\d{4}-\d{1}"/> </xsd:restriction> </xsd:simpleType> |
利用这个自定义的简单类型,我们可以重新写Schema文档:
order4.xsd
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="order"> <xsd:complexType> <xsd:sequence> <xsd:element ref="orderItem" maxOccurs="10"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="orderItem"> <xsd:complexType> <xsd:sequence> <xsd:element name="id" type="idType"/> <xsd:element name="quantity" type="xsd:integer"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:simpleType name="idType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{1}-\d{4}-\d{4}-\d{1}"/> </xsd:restriction> </xsd:simpleType> </xsd:schema> |
1 2 3 4 5 6 7 |
<xsd:simpleType name="idType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="7-5058-3496-7"/> <xsd:enumeration value="7-5005-6450-3"/> <xsd:enumeration value="7-3020-6069-7"/> </xsd:restriction> </xsd:simpleType> |
1 2 3 4 5 6 |
<xsd:simpleType name="quantityType"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="1"/> <xsd:maxInclusive value="10"/> </xsd:restriction> </xsd:simpleType> |
order4-1.xsd
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="order"> <xsd:complexType> <xsd:sequence> <xsd:element ref="orderItem" maxOccurs="10"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="orderItem"> <xsd:complexType> <xsd:sequence> <xsd:element name="id" type="idType"/> <xsd:element name="quantity" type="quantityType"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:simpleType name="idType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="7-5058-3496-7"/> <xsd:enumeration value="7-5005-6450-3"/> <xsd:enumeration value="7-3020-6069-7"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="quantityType"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="1"/> <xsd:maxInclusive value="10"/> </xsd:restriction> </xsd:simpleType> </xsd:schema> |
最后,我们再来讲讲元素的属性如何在Schema文档中定义。比如上面的order.xml实例文档中:
1 2 3 |
<order> <orderItem id="7-5058-3496-7" /> </order> |
order.xsd
1 2 3 4 5 6 7 8 9 |
<xsd:element name="orderItem"> <xsd:complexType> <xsd:sequence> <!--空元素--> </xsd:sequence> <!--定义该元素属性--> <xsd:attribute name="id" type="xsd:string"/> </xsd:complexType> </xsd:element> |
1 | <xsd:attribute name="id" type="idType" use="required"/> |
1 2 3 |
<order> <orderItem id="4-8443-1780-6" quantity="3"/> </order> |
1 | <xsd:attribute name="quantity" type="xsd:integer" default="1"/> |
order2.xsd
1 2 3 4 5 6 7 |
<xsd:element name="orderItem"> <xsd:complexType> <xsd:sequence></xsd:sequence> <xsd:attribute name="id" type="idType" use="required"/> <xsd:attribute name="quantity" type="xsd:integer" default="1"/> </xsd:complexType> </xsd:element> |
order3.xsd
01 02 03 04 05 06 07 08 09 10 |
<xsd:element name="orderItem"> <xsd:complexType> <xsd:sequence></xsd:sequence> <xsd:attributeGroup ref="orderItemAttributes" /> </xsd:complexType> </xsd:element> <xsd:attributeGroup name="orderItemAttributes"> <xsd:attribute name="id" type="idType" use="required" /> <xsd:attribute name="quantity" type="xsd:integer" default="1" /> </xsd:attributeGroup> |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="order"> <xsd:complexType> <xsd:sequence> <xsd:element ref="orderItem" maxOccurs="10" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="orderItem"> <xsd:complexType> <xsd:sequence></xsd:sequence> <xsd:attributeGroup ref="orderItemAttributes" /> </xsd:complexType> </xsd:element> <xsd:attributeGroup name="orderItemAttributes"> <xsd:attribute name="id" type="idType" use="required" /> <xsd:attribute name="quantity" type="xsd:integer" default="1" /> </xsd:attributeGroup> <xsd:simpleType name="idType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{1}-\d{4}-\d{4}-\d{1}" /> </xsd:restriction> </xsd:simpleType> </xsd:schema> |