定义子元素的顺序:
1、任意顺序<xs:all>:
指定子元素在其父元素中出现时,顺序任意,但每种子元素最多只能出现一次。
例:
Schema文档:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:all>
<xs:element name="id" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="011.xsd">
<author>李四</author>
<name>XML学习天下</name>
<id>001</id>
<price>88.50</price>
</book>
2、特定顺序<xs:sequence>:
指定子元素在其父元素中出现时,顺序必须与在Schema文档中定义的顺序一致。
例:
Schema文档:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="009.xsd">
<id>001</id>
<name>XML学习天下</name>
<author>李四</author>
<price>88.50</price>
</book>
3、选择顺序<xs:choice>:
指定在多种子元素中,只能有一种出现。
例:
Schema文档:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:choice>
<xs:element name="id" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="012.xsd">
<name>XML学习天下</name>
</book>
4、顺序嵌套:
<xs:sequence>可以嵌套<xs:choice>。
例:
Schema文档:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="id" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
</xs:choice>
<xs:choice>
<xs:element name="author" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="013.xsd">
<id>001</id>
<price>88.50</price>
</book>
说明:这三种指示符用来定义子元素在其父元素中出现的顺序。
定义子元素出现的次数:
格式:
<xs:element name="元素名称" type="元素类型" minOccurs="最小值" maxOccurs="最大值"/>
说明:
minOccurs:
定义子元素最少出现的次数,最小可以为0,表示不出现。
maxOccurs:
定义子元素最多出现的次数,最大可以为unbounded,表示不限制出现次数。
例:
Schema文档:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:integer" minOccurs="0" maxOccurs="0"/>
<xs:element name="name" type="xs:string" maxOccurs="1"/>
<xs:element name="author" type="xs:string" minOccurs="1" maxOccurs="3"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="014.xsd">
<name>XML学习天下</name>
<author>张三</author>
<author>李四</author>
<author>赵五</author>
<price>88.50</price>
</book>
注:
在没有指定minOccurs和maxOccurs的情况下,其值默认值都为1,表示必须出现一次,且只能出现一次。
<xs:all>任意顺序指示符,不可以将其内部声明的元素的maxOccurs设为大于1的数值。
1、任意顺序<xs:all>:
指定子元素在其父元素中出现时,顺序任意,但每种子元素最多只能出现一次。
例:
Schema文档:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:all>
<xs:element name="id" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="011.xsd">
<author>李四</author>
<name>XML学习天下</name>
<id>001</id>
<price>88.50</price>
</book>
2、特定顺序<xs:sequence>:
指定子元素在其父元素中出现时,顺序必须与在Schema文档中定义的顺序一致。
例:
Schema文档:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="009.xsd">
<id>001</id>
<name>XML学习天下</name>
<author>李四</author>
<price>88.50</price>
</book>
3、选择顺序<xs:choice>:
指定在多种子元素中,只能有一种出现。
例:
Schema文档:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:choice>
<xs:element name="id" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="012.xsd">
<name>XML学习天下</name>
</book>
4、顺序嵌套:
<xs:sequence>可以嵌套<xs:choice>。
例:
Schema文档:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="id" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
</xs:choice>
<xs:choice>
<xs:element name="author" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="013.xsd">
<id>001</id>
<price>88.50</price>
</book>
说明:这三种指示符用来定义子元素在其父元素中出现的顺序。
定义子元素出现的次数:
格式:
<xs:element name="元素名称" type="元素类型" minOccurs="最小值" maxOccurs="最大值"/>
说明:
minOccurs:
定义子元素最少出现的次数,最小可以为0,表示不出现。
maxOccurs:
定义子元素最多出现的次数,最大可以为unbounded,表示不限制出现次数。
例:
Schema文档:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:integer" minOccurs="0" maxOccurs="0"/>
<xs:element name="name" type="xs:string" maxOccurs="1"/>
<xs:element name="author" type="xs:string" minOccurs="1" maxOccurs="3"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML文档:
<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="014.xsd">
<name>XML学习天下</name>
<author>张三</author>
<author>李四</author>
<author>赵五</author>
<price>88.50</price>
</book>
注:
在没有指定minOccurs和maxOccurs的情况下,其值默认值都为1,表示必须出现一次,且只能出现一次。
<xs:all>任意顺序指示符,不可以将其内部声明的元素的maxOccurs设为大于1的数值。