XML Schema快速入门(三)语法之复杂类型
系列专题
XML Schema快速入门(一)如何使用
XML Schema快速入门(二)语法之简单类型
XML Schema快速入门(三)语法之复杂类型
接下来讲解复杂类型。
可以简单理解为复杂类型就是用来定义复合元素的。
1. 复合元素
首先理解下什么是复合元素
复合元素指包含其他元素及/或属性的 XML 元素。
有四种类型的复合元素:
- 空元素
<product pid="1345"/>
- 仅包含其他元素的元素
<employee><firstname>John</firstname><lastname>Smith</lastname>
</employee>
- 仅包含文本的元素
<food type="dessert">Ice cream</food>
- 包含元素和文本的元素
<description>
It happened on <date lang="norwegian">03.03.99</date> ....
</description>
以上元素都是可以有属性的。
2. <xsd:complexType> 元素
复合元素是通过复杂数据类型(complexType元素)来定义的,所以我们先讲complexType元素。
先看定义:
定义一个复杂类型,它确定属性集和元素内容。
语法:
<complexType
abstract = Boolean : false
block = (#all | List of (extension | restriction))
final = (#all | List of (extension | restriction))
id = ID
mixed = Boolean : false
name = NCName
{any attributes with non-schema Namespace…}>
Content: (annotation?, (simpleContent | complexContent | ((group | all |
choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))))
</complexType>
属性:
属性 | 值 |
---|---|
abstract |
一个指示器,指示在实例文档中是否可以使用复杂类型。如果该值为 true,则元素不能直接使用该复杂类型,而是必须使用从该复杂类型派生的复杂类型。默认值为 false。
可选项。 |
block |
派生的类型。block 属性防止具有指定派生类型的复杂类型被用来替代该复杂类型。该值可以包含 #all 或者一个列表,该列表是 extension 或 restriction 的子集。仅当验证期间实例文档通过使用 schema-instance:type 属性重写元素的普通类型时,才使用 block 属性。block 属性能够防止元素选择复杂类型,这些复杂类型通过扩展和/或限制进行定义以替代为该元素指定的原始类型。
extension:防止通过扩展派生的复杂类型被用来替代该复杂类型。 可选项。 |
final |
派生的类型。final 属性防止从该 complexType 元素派生指定的类型。该值可以包含 #all 或者一个列表,该列表是 extension 或 restriction 的子集。
extension:防止通过扩展派生。 可选项。 |
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
mixed |
一个指示符,指示是否允许字符数据出现在该复杂类型的子元素之间。默认值为 false。 如果 simpleContent 元素是子元素,则不允许 mixed 属性。 如果 complexContent 元素是子元素,则该 mixed 属性可被 complexContent 元素的 mixed 属性重写。 可选项。 |
name |
类型名称。该名称必须是在 XML 命名空间规范中定义的无冒号名称 (NCName)。 如果包含元素是 schema 元素,则为必选项;否则将被禁止。 如果指定,该名称在所有 simpleType 和 complexType 元素之间必须是唯一的。 可选项。 |
元素信息:
出现次数:在架构内为无限制;在元素内为一次。
父元素:element、redefine、schema
内容:annotation、simpleContent、complexContent、group、all、choice、sequence、attribute、attributeGroup、anyAttribute
用法:
复杂类型实质上是对可以包含属性和元素的元素的类型定义。一个元素可以由引用 complexType 元素(定义该元素的结构、内容和属性)的 type 属性声明。(一个元素还可以在其 type 属性中接受对 simpleType 的引用。)
一个复杂类型可以包含以下元素中的一个元素且只能包含一个元素,该元素确定在复杂类型中允许的内容类型。
元素 | 说明 |
---|---|
simpleContent | 该复杂类型具有字符数据或将 simpleType 作为内容并且不包含任何元素,但可以包含属性。 |
complexContent | 该复杂类型只包含元素或不包含任何元素内容(空)。 |
group | 该复杂类型包含在引用组中定义的元素。 |
sequence | 该复杂类型包含在指定序列中定义的元素。 |
choice | 该复杂类型允许在选项元素中指定的元素之一。 |
all | 该复杂类型允许在所有元素中指定的任意或所有元素出现一次。 |
如果 group、sequence、choice 或 all 被指定为子元素,则可以选择使用以下元素声明 complexType 的属性。
元素 | 说明 |
---|---|
attribute | 该复杂类型包含指定的属性。 |
attributeGroup | 该复杂类型包含在引用的 attributeGroup 中定义的属性。 |
anyAttribute | 该复杂类型可以包含来自指定命名空间的任何属性。 |
可以使用任何数目的 attribute 或 attributeGroup 元素。还可以使用 anyAttribute 的一个实例。
如果指定 group、sequence、choice 或 all,则元素必须按以下顺序出现。
- group | sequence | choice | all
- attribute | attributeGroup
- anyAttribute
2.1 <xsd:simpleContent> 元素
使用该元素定义的复杂类型具有字符数据或将 simpleType 作为内容并且不包含任何元素,但可以包含属性。
定义:
包含对 complexType 元素(它以字符数据或 simpleType 元素为内容)的扩展或限制并且不包含任何元素。
语法:
<simpleContent
id = ID
{any attributes with non-schema Namespace}…>
Content: (annotation?, (restriction | extension))
</simpleContent>
属性:
属性 | 值 |
---|---|
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
元素信息:
出现次数:一次
父元素:complexType
内容:可选项 — annotation
必选项 — 有并且只有一个下列元素:restriction (simpleContent) 或 extension (simpleContent)。
用法:
simpleContent 元素可以将一个元素指定为包含其中没有任何元素的 simpleType,并且可以限制该元素的内容的值或通过属性扩展该元素
必须通过下列方法之一定义简单内容。
- restriction 元素
将元素值的范围限制为继承的 simpleType 的那些值的子集。 - extension 元素
通过添加属性扩展元素的 simpleType 内容。
2.1.1 <xsd:restriction> 元素 (simpleContent)
定义对 simpleContent 定义的约束。
语法:
<restriction
base = QName
id = ID
{any attributes with non-schema Namespace}…>
Content: (annotation?, (simpleType?, (minExclusive | minInclusive |
maxExclusive | maxInclusive | totalDigits |fractionDigits | length |
minLength | maxLength | enumeration | whiteSpace | pattern))?,
((attribute | attributeGroup), anyAttribute?))
</restriction>
属性:
属性 | 值 |
---|---|
base |
在该架构(或由指定的命名空间指示的其他架构)中定义的内置数据类型、simpleType 元素或 complexType 元素的名称。包含 restriction 元素的元素是从基值所指定的类型派生的。
base 是内置数据类型、简单类型或复杂类型的名称。如果是复杂类型,则该类型必须是以下类型之一:内置数据类型、简单类型或简单内容。 基值必须是限定名 (QName)。 必选项。 |
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
元素信息:
出现次数:一次
父元素:simpleContent
内容 (simpleContent):annotation、fractionDigits、enumeration、length、maxExclusive、maxInclusive、maxLength、minExclusive、minInclusive、minLength、pattern、simpleType、totalDigits、whiteSpace、attribute、attributeGroup、anyAttribute
例子:
以下示例说明 restriction 限制其他的 simpleContent 扩展。值集被限制为值和属性的短列表。
<xs:simpleType name="mountainbikesize"><xs:restriction base="xs:string"><xs:enumeration value="small"/><xs:enumeration value="medium"/><xs:enumeration value="large"/></xs:restriction></xs:simpleType><xs:complexType name="FamilyMountainBikeSizes"><xs:simpleContent><!-- mountainbikesize基础上添加属性 --><xs:extension base="mountainbikesize"><xs:attribute name="familyMember" type="xs:string"/></xs:extension></xs:simpleContent></xs:complexType><xs:complexType name="ChildMountainBikeSizes"><xs:simpleContent><!-- FamilyMountainBikeSizes基础上限制可选值 --><xs:restriction base="FamilyMountainBikeSizes"><xs:enumeration value="small"/><xs:enumeration value="medium"/></xs:restriction></xs:simpleContent></xs:complexType><!-- ChildMountainBikeSizes值集为small,medium以及属性familyMember
2.1.2 <xsd:extension> 元素 (simpleContent)
该复杂类型只包含元素或不包含任何元素内容(空)。
包含对 simpleContent 的扩展。通过添加指定属性、属性组或 anyAttribute,扩展了简单类型或具有简单内容的复杂类型。
语法:
<extension
base = QName
id = ID
{any attributes with non-schema Namespace}…>
Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
</extension>
属性:
属性 | 值 |
---|---|
base |
内置数据类型、simpleType 元素或具有简单内容的 complexType 的名称。
基值必须是限定名 (QName)。 必选项。 |
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
元素信息:
出现次数:一次
父元素:simpleContent
内容:annotation、attribute、attributeGroup、anyAttribute
例子:
以下示例通过添加枚举的属性扩展定义的 simpleType。
<xs:simpleType name="mountainBikeSize"><xs:restriction base="xs:string"><xs:enumeration value="small"/><xs:enumeration value="medium"/><xs:enumeration value="large"/></xs:restriction></xs:simpleType><xs:complexType name="FamilyMountainBikes"><xs:simpleContent><xs:extension base="mountainBikeSize"><!-- 在mountainBikeSize的基础上扩展了属性 --><xs:attribute name="familyMember"><xs:simpleType><!-- 并且属性值限定为child、male、female --><xs:restriction base="xs:string"><xs:enumeration value="child"/><xs:enumeration value="male"/><xs:enumeration value="female"/></xs:restriction></xs:simpleType></xs:attribute></xs:extension></xs:simpleContent></xs:complexType><!-- FamilyMountainBikes值集为small、medium、large以及属性familyMember -->
2.2 <xsd:complexContent> 元素
定义:
包含对复杂类型(包含混合内容或仅包含元素)的扩展或限制。
语法:
<complexContent
id = ID
mixed = Boolean
{any attributes with non-schema Namespace}…>
Content: (annotation?, (restriction | extension))
</complexContent>
属性:
属性 | 值| |
---|---|
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
mixed |
一个指示符,指示是否允许字符数据出现在该 complexType 元素的子元素之间。默认值为 false。
该 mixed 属性可以重写在包含 complexType 元素上指定的 mixed 属性值。 可选项。 |
元素信息:
出现次数:一次
父元素:complexType
内容:
- 可选项。annotation
- 必选项。有并且只有一个下列元素:restriction (complexContent) 或 extension (complexContent)。
用法:
和simpleContent有点类似,但是还是有区别对,必须通过下列方法之一定义复杂内容:
- restriction 元素
- extension 元素
2.2.1 <xsd:restriction> 元素 (complexContent)
定义对 complexContent 定义的约束。
语法:
<restriction
base = QName
id = ID
{any attributes with non-schema Namespace}…>
Content: (annotation?, (group | all | choice | sequence)?, ((attribute |
attributeGroup)*, anyAttribute?))
</restriction>
属性:
属性 | 值 |
---|---|
base |
在该架构(或由指定的命名空间指示的其他架构)中定义的 complexType 元素的名称。包含 restriction 元素的元素是从基值所指定的类型派生的。
基值必须是限定名 (QName)。 必选项。 |
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
元素信息:
出现次数:一次
父元素:complexContent
内容:group、all、choice、sequence、attribute、attributeGroup、anyAttribute
例子:
以下示例说明使用 restriction 的复杂类型定义。复杂类型 USAddress 是从常规地址复杂类型派生的,并且其 country 元素被固定为 US,且删除掉了zipcode元素。
<xs:complexType name="address"><xs:sequence><xs:element name="street" type="xs:string" /><xs:element name="city" type="xs:string" /><xs:element name="zipcode" type="xs:integer" /><xs:element name="country" type="xs:string" /></xs:sequence>
</xs:complexType><xs:complexType name="USAddress"><xs:complexContent><xs:restriction base="address"><xs:sequence><xs:element name="street" type="xs:string" /><xs:element name="city" type="xs:string" /><xs:element name="country" type="xs:string" fixed="US" /></xs:sequence></xs:restriction></xs:complexContent>
</xs:complexType>
2.2.2 <xsd:extension> 元素 (complexContent)
包含对 complexContent 的扩展。
语法:
<extension
base = QName
id = ID
{any attributes with non-schema Namespace}…>
Content: (annotation?, ((group | all | choice | sequence)?, ((attribute |
attributeGroup)*, anyAttribute?)))
</extension>
属性:
属性 | 值 |
---|---|
base |
complexType 元素的名称。
基值必须是限定名 (QName)。 必选项。 |
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
元素信息:
出现次数:一次
父元素:complexContent
内容:annotation、attribute、attributeGroup、anyAttribute、choice、all、sequence、group
例子:
以下示例通过添加一个元素和一个属性扩展已定义的 complexType 元素。
<xs:complexType name="address"><xs:sequence><xs:element name="street" type="xs:string"/><xs:element name="city" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="USAddress"><xs:complexContent><!-- 在address基础上添加state元素和country属性 --><xs:extension base="address"><xs:sequence><xs:element name="state" type="xs:string"/></xs:sequence><xs:attribute name="country" type="xs:string" fixed="US"/></xs:extension></xs:complexContent></xs:complexType>
2.3 <xsd:all> 元素
允许组中的元素以任意顺序显示(或不显示)在包含元素中。
属性:
<all
id = ID
maxOccurs= 1: 1
minOccurs= (0 | 1): 1
{any attributes with non-schema Namespace…}>
Content: (annotation?, element*)
</all>
属性:
属性 | 值 |
---|---|
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
maxOccurs |
元素可出现的最大次数。该值必须是 1。
可选项。 |
minOccurs |
元素可出现的最小次数。该值可以是整数 1。若要指定该元素是可选的,请将该属性设置为 0。默认值为 1。
可选项。 |
元素信息:
出现次数:一次
父元素:group、restriction (simpleContent)、extension (simpleContent)、restriction (complexContent)、extension (complexContent)、complexType
内容:annotation、element
例子:
下面的示例定义一个复杂数据类型,包含 all 元素以及下面示例的 XML 实例文档的三个元素:
<!-- 架构文档 -->
<xs:element name="thing1" type="xs:string"/>
<xs:element name="thing2" type="xs:string"/>
<xs:element name="thing3" type="xs:string"/><xs:attribute name="myAttribute" type="xs:decimal"/><xs:complexType name="myComplexType"><xs:all><xs:element ref="thing1"/><xs:element ref="thing2"/><xs:element ref="thing3"/></xs:all><xs:attribute ref="myAttribute"/>
</xs:complexType><!-- 架构实例 -->
<?xml version="1.0"?>
<myElement myAttribute="1.1"><!-- 元素可以任意顺序 --><thing2>Some</thing2><thing3>text</thing3><thing1>for you</thing1>
</myElement>
2.4 <xsd:sequence> 元素
要求组中的元素以指定的顺序出现在包含元素中。
语法:
<sequence
id = ID
maxOccurs = (nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
{any attributes with non-schema Namespace}…>
Content: (annotation?, (element | group | choice | sequence | any)*)
</sequence>
属性:
属性 | 值 |
---|---|
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
maxOccurs |
序列可出现的最大次数。该值可以是大于或等于零的整数。若不想对最大次数设置任何限制,请使用字符串“unbounded”。
可选项。 |
minOccurs |
序列可出现的最小次数。该值可以是大于或等于零的整数。若要指定该序列组是可选的,请将此属性设置为零。
可选项。 |
元素信息:
出现次数:在组内为一次;否则为无限制。
父元素:group、choice、sequence、complexType、restriction (simpleContent)、extension (simpleContent)、restriction (complexContent)、extension (complexContent)
内容:annotation、any、choice、element、group、sequence
例子:
以下示例说明在 sequence 元素中可以包含下列元素的零个或多个:elephant、bear、giraffe。
<xs:complexType name="zoo"><xs:sequence minOccurs="0" maxOccurs="unbounded"><xs:element name="elephant" type="xs:string"/><xs:element name="bear" type="xs:string"/><xs:element name="giraffe" type="xs:string"/></xs:sequence></xs:complexType>
注意elephant、bear、giraffe三个元素按顺序排列,并且三个为一个组,需要同时出现,对应的架构实例应该是这个样子:
<?xml version="1.0" encoding="UTF-8" ?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="dataTypeTest.xsd"xsi:type="type1"><name><elephant>11</elephant><!-- <elephant>22</elephant> 插入这个是不被允许的 --><bear>11</bear><giraffe>11</giraffe><elephant>22</elephant><bear>22</bear><giraffe>33</giraffe><!-- .... --></name>
</test>
2.5 <xsd:choice> 元素
允许且仅允许选定组中包含的一个元素出现在包含元素中。
语法:
<choice
id = ID
maxOccurs= (nonNegativeInteger | unbounded) : 1
minOccurs= nonNegativeInteger : 1
{any attributes with non-schema Namespace}…>
Content: (annotation?, (element | group | choice | sequence | any)*)
</choice>
属性:
属性 | 值 |
---|---|
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
maxOccurs |
可进行选择的最大次数。该值可以是大于或等于零的整数。若不想对最大次数设置任何限制,请使用字符串“unbounded”。默认值为 1。
可选项。 |
minOccurs |
可进行选择的最小次数。该值可以是大于或等于零的整数。若要指定该选项组是可选的,请将此属性设置为 zero。默认值为 1。
可选项。 |
元素信息:
出现次数:在 group 和 complexType 元素中为一次;其他为无限制。
父元素:group、choice、sequence、complexType、restriction (simpleContent)、extension (simpleContent)、restriction (complexContent)、extension (complexContent)
内容:annotation、any、choice、element、group、sequence
例子:
以下复杂类型定义一个元素,该元素具有一个属性,并且有一个且仅有一个元素来自四个指定元素的选择。
<xs:complexType name="testchoice"><xs:choice minOccurs="1" maxOccurs="1"><xs:element name="a" type="xs:string"/><xs:element name="b" type="xs:string"/><xs:element name="c" type="xs:string"/><xs:element name="d" type="xs:string"/></xs:choice><xs:attribute name="age" type="xs:integer"/></xs:complexType>
2.5.1 <xsd:any> 元素
使来自指定命名空间的任何元素可以显示在包含 sequence 或 choice 元素中。
语法:
<any
id = ID
maxOccurs = (nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
namespace = "(##any | ##other) | List of (anyURI | (##targetNamespace | ##local))) : ##any
processContents = (lax | skip | strict) : strict
{any attributes with non-schema Namespace…}>
Content: (annotation?)
</any>
属性:
属性 | 值 |
---|---|
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
maxOccurs |
any 元素在元素上可以出现的最大次数。该值可以是大于或等于零的整数。若不想对最大次数设置任何限制,请使用字符串“unbounded”。默认值为 1。
可选项。 |
minOccurs |
any 元素在元素上可以出现的最小次数。该值可以是大于或等于零的整数。若要指定该 any 组是可选的,请将此属性设置为零。默认值为 1。
可选项。 |
namespace |
包含可以使用的元素的命名空间。如果没有指定命名空间,则 ##any 为默认值。如果指定命名空间,则必须是以下值之一。
可选项。 |
processContents |
一个指示符,指示应用程序或 XML 处理器应如何根据由该 any 元素指定的元素处理 XML 文档的验证。如果没有指定 processContents 属性,则默认为 strict。如果指定了 processContents,必须是以下值之一。
可选项。 |
元素信息:
出现次数:无限制
父元素:choice、sequence
内容:annotation
例子:
以下示例说明一个元素的元素声明,该元素必须包含来自 XHTML 命名空间的一个或多个元素,同时内容处理被设置为 lax。
<xs:element name='htmlText'><xs:complexType><xs:sequence><xs:any namespace='http://www.w3.org/1999/xhtml'minOccurs='1' maxOccurs='unbounded'processContents='lax'/></xs:sequence></xs:complexType>
</xs:element>
下面这个例子是从名为 “family.xsd” 的 XML schema 中引用的片段。它展示了一个针对 “person” 元素的声明。通过使用 <any> 元素,我们可以通过任何元素(在 <lastname> 之后)扩展 “person” 的内容:
<xs:element name="person"><xs:complexType><xs:sequence><xs:element name="firstname" type="xs:string"/><xs:element name="lastname" type="xs:string"/><xs:any minOccurs="0"/></xs:sequence></xs:complexType>
</xs:element>
现在,我们希望使用 “children” 元素来扩展 “person” 元素。这此种情况下我们就可以这么做,即使以上这个 schema 的作者没有声明任何 “children” 元素。
请看这个 schema 文件,名为 “children.xsd”:
<?xml version="1.0" encoding="ISO-8859-1"?>
<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:element name="children"><xs:complexType><xs:sequence><xs:element name="childname" type="xs:string"maxOccurs="unbounded"/></xs:sequence></xs:complexType>
</xs:element></xs:schema>
下面这个 XML 文件(名为 “Myfamily.xml”),使用了来自两个不同的 schema 中的成分,“family.xsd” 和 “children.xsd”:
<?xml version="1.0" encoding="ISO-8859-1"?><persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
http://www.w3school.com.cn children.xsd"><person><firstname>David</firstname><lastname>Smith</lastname><children><childname>mike</childname></children>
</person><person><firstname>Tony</firstname><lastname>Smith</lastname>
</person></persons>
上面这个 XML 文件是有效的,这是由于 schema “family.xsd” 允许我们通过在 “lastname” 元素后的可选元素来扩展 “person” 元素。
2.6 <xsd:group> 元素
将若干元素声明归为一组,以便将它们当作一个组并入复杂类型定义。
语法:
<group
name= NCName
id = ID
maxOccurs = (nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
name = NCName
ref = QName
{any attributes with non-schema Namespace}…>
Content: (annotation?, (all | choice | sequence))
</group>
属性:
属性 | 值 |
---|---|
name |
组的名称。该名称必须是在 XML 命名空间规范中定义的无冒号名称 (NCName)。
仅当 schema 元素是该 group 元素的父元素时才使用该属性。在此情况下,group 是由 complexType、choice 和 sequence 元素使用的模型组。 可选项。 |
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
maxOccurs |
该元素可以在包含元素中出现的最大次数。该值可以是大于或等于零的整数。若不想对最大次数设置任何限制,请使用字符串“unbounded”。
当该组不是 schema 元素的子级时,将受到限制。 可选项。 |
minOccurs |
该元素可以在包含元素中出现的最少次数。该值可以是大于或等于零的整数。若要指定该元素是可选的,请将此属性设置为零。
当该组不是 schema 元素的子级时,将受到限制。 可选项。 |
name |
元素的名称。该名称必须是在 XML 命名空间规范中定义的无冒号名称 (NCName)。Name 和 ref 属性不能同时出现。
如果包含元素是 schema 元素,则是必选项。 可选项。 |
ref |
在该架构(或由指定的命名空间指示的其他架构)中声明的组的名称。ref 值必须是 QName。ref 可以包含命名空间前缀。
如果 ref 属性出现,则 id、minOccurs 和 maxOccurs 可以出现。Ref 和 name 是互相排斥的。 若要使用现有组定义声明一个组,请使用 ref 属性指定现有组定义。 可选项。 |
元素信息:
出现次数:无限制
父元素:schema、choice、sequence、complexType、restriction (complexContent)、extension (complexContent)
内容:annotation、all、choice、sequence
用法注意:
1.group元素既用来申明组,又用来引用组。ref和name属性是排斥的,一个引用一个申明。
2.以下各元素中的任何一个元素都可以是 group 元素的子元素,且同时只能存在一个。
- choice
允许在组内包含元素之一且只能包含一个。 - sequence
要求组中的元素以指定顺序显示。 - all
允许组中的元素以任意顺序显示(或不显示)在包含元素中。
3.group元素引用的时候,即ref属性出现的时候,才可以使用minOccurs和maxOccurs属性,并且minOccurs 和 maxOccurs 属性的默认值为 1。没有这两个属性的组在包含元素中必须出现一次并且只能出现一次。
例子:
以下示例定义一个包含三个元素的序列的组并且在复杂类型定义中使用 group 元素。
<xs:element name="thing1" type="xs:string"/><xs:element name="thing2" type="xs:string"/><xs:element name="thing3" type="xs:string"/><xs:attribute name="myAttribute" type="xs:decimal"/><!-- 定义序列组 --><xs:group name="myGroupOfThings"><xs:sequence><xs:element ref="thing1"/><xs:element ref="thing2"/><xs:element ref="thing3"/></xs:sequence></xs:group><xs:complexType name="myComplexType"><!-- 引用组 --><xs:group ref="myGroupOfThings"/><xs:attribute ref="myAttribute"/></xs:complexType>
2.7 <xsd:attribute> 元素
声明一个属性。
语法:
<attribute
default = string
fixed = string
form = (qualified | unqualified)
id = ID
name = NCName
ref = QName
type = QName
use = (optional | prohibited | required): optional
{any attributes with non-schema Namespace…}>
Content: (annotation?, (simpleType?))
</attribute>
属性:
属性 | 值 |
---|---|
default |
属性具有默认值。如果此属性不在 XML 文档内的实例中指定,则此属性具有给定的值。如果此属性未出现在实例文档中,则此属性应出现在后架构验证信息集中。如果属性实际上不在实例文档中,则架构的处理器应当表现为如同该属性已指定为默认值一样。Default 和 fixed 属性不能同时出现。
可选项。 |
fixed |
属性具有固定值。如果此属性出现于实例文档中,则其值必须等于给出的固定值。如果此属性不出现,则此属性接收提供的值。Default 和 fixed 属性不能同时出现。
可选项。 |
form |
属性的格式。默认值是包含该属性的 schema 元素的 attributeFormDefault 属性的值。该值必须是下列字符串之一:“qualified”或“unqualified”。
如果该值是非限定的,则此属性无须由命名空间前缀限定并且匹配此属性的无冒号名称 (NCName),即本地名称。 如果该值是限定的,则必须通过将架构的 targetNamespace 与属性的 NCName 相组合来限定此属性。 可选项。 |
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
name |
属性名。该名称必须是在 XML 命名空间规范中定义的 NCName。如果包含元素是 schema 元素,则是必选项。Name 和 ref 属性不能同时出现。
当根据一个架构对 XML 文档进行验证时,根据该架构中的 attribute 元素验证文档中的每一属性。 可选项。 |
ref |
在该架构(或由指定的命名空间指示的其他架构)中声明的属性的名称。ref 值必须是限定名 (QName)。类型可以包括命名空间前缀。Name 和 ref 属性不能同时出现。如果 ref 出现,则 simpleType 元素、form 和 type 不能出现。
若要在复杂类型内使用现有属性定义声明一个属性,请使用 ref 属性指定现有属性定义。<xs:attribute name=“mybaseattribute”> 可选项。 |
type |
在该架构(或由指定的命名空间指示的其他架构)中定义的内置数据类型或简单类型的名称。该类型必须是 QName。类型可以包括命名空间前缀。type 属性只能在内容不包含 simpleType 元素时出现。
可选项。 |
use |
一个指示符,指示如何使用属性。
如果指定,该属性必须具有以下值之一。
可选项。 |
元素信息:
出现次数:在 schema 元素中定义一次。在复杂类型或属性组中引用多次。
父元素:attributeGroup、schema、complexType、restriction (simpleContent)、extension (simpleContent)、restriction (complexContent)、extension (complexContent)
内容:annotation、simpleType
备注:
属性声明将名称与类型定义关联,类型定义可以是内置数据类型或简单类型。
属性声明可作为 schema、complexType 和 attributeGroup 元素(具有全局范围)的子元素出现,或在复杂类型定义内出现。对于复杂类型,属性声明可作为本地声明或对具有全局范围的属性的引用出现。
此外,属性可以在 attributeGroup 和 complexType 元素内的引用中出现。
例子:
在以下示例中,通过对内置类型的引用声明一个属性,该属性的默认值为 test,该属性用在 complexType 元素中。
<xs:attribute name="mybaseattribute" type="xs:string" default="test" />
<xs:complexType name="myComplexType"><xs:attribute ref="mybaseattribute"/>
</xs:complexType>
以下示例中在 complexType 元素内直接声明了一个 required 属性。
<xs:complexType name="myComplexType"><xs:attribute name="mybaseattribute" type="xs:string" use="required"/>
</xs:complexType>
以下示例中声明了一个属性,具体方法是:从内置 integer 类型(通过限制)进行派生,将值的范围限制在 “60” 和 “95” 之间(包括 60 和 95)。
<xs:attribute name="myHolidayLocationTemperature"><xs:simpleType><xs:restriction base="xs:integer"><xs:minInclusive value="60"/><xs:maxInclusive value="95"/></xs:restriction></xs:simpleType>
</xs:attribute>
在以下示例中,属性被声明为包含十进制数值的列表。(这允许诸如 shoeSizes=“10.5 9 8 11” 之类的属性包含值 10.5、9、8 和 11 的列表)。
<xs:simpleType name="Sizes"><xs:restriction base="xs:decimal"><xs:enumeration value="10.5"/><xs:enumeration value="9"/><xs:enumeration value="8"/><xs:enumeration value="11"/></xs:restriction></xs:simpleType> <xs:attribute name="shoeSizes"><xs:simpleType><xs:list itemType="Sizes"/></xs:simpleType></xs:attribute>
2.8 <xsd:attributeGroup> 元素
将一组属性声明归为一组,以便为复杂类型定义将它们并为一个组。
语法:
<attributeGroup
id = ID
name = NCName
ref = QName
{any attributes with non-schema Namespace…}>
Content: (annotation?), ((attribute | attributeGroup)*, anyAttribute?))
</attributeGroup>
属性:
属性 | 值 |
---|---|
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
name |
其属性包括在 attributeGroup 元素或 complexType 元素中的属性组的名称。该名称必须是在 XML 命名空间规范中定义的无冒号名称 (NCName)。Name 和 ref 属性不能同时出现。只在该属性组是 schema 元素的子级时才可以出现 Name 属性。
可选项。 |
ref |
其属性包括在 attributeGroup 元素或 complexType 元素中的属性组的引用名称。
Name 和 ref 属性不能同时出现。只在该属性组不是 schema 元素的子级时才可以出现 Ref 属性。 该值必须是限定名 (QName)。 可选项。 |
元素信息:
出现次数:无限制
父元素:attributeGroup、complexType、schema、restriction (simpleContent)、extension (simpleContent)、restriction (complexContent)、extension (complexContent)
内容:annotation、attribute、attributeGroup、anyAttribute
备注:
属性组只可以被定义为 schema 元素的子级。在这种情况下,name 属性必须出现并且包含构成该属性组的 attribute、attributeGroup 或 anyAttribute 元素。
属性组可以包括在 attributeGroup 或 complexType 中;在这种情况下,ref 属性必须出现并且 attributeGroup 元素必须为空。
attributeGroup 元素可以包含其他 attributeGroup 元素。
用法:
简单说就是name属性定义组的时候用,ref属性引用的时候用,且只能在schema元素子级下才能定义,只能在attributeGroup 或 complexType 中引用
例子:
以下示例说明在复杂类型 (myElementType) 中定义和使用的属性组 (myAttributeGroup)。
<xs:attributeGroup name="myAttributeGroup"><xs:attribute name="someattribute1" type="xs:integer"/><xs:attribute name="someattribute2" type="xs:string"/>
</xs:attributeGroup><xs:complexType name="myElementType"><xs:attributeGroup ref="myAttributeGroup"/>
</xs:complexType>
以下示例说明两个属性组(myAttributeGroupA 和 myAttributeGroupB),并且将这两个属性组定义为其中一个属性组包含另一个属性组。
<xs:attributeGroup name="myAttributeGroupA"><xs:attribute name="someattribute10" type="xs:integer"/><xs:attribute name="someattribute11" type="xs:string"/>
</xs:attributeGroup><xs:attributeGroup name="myAttributeGroupB"><xs:attribute name="someattribute20" type="xs:date"/><xs:attributeGroup ref="myAttributeGroupA"/>
</xs:attributeGroup>
2.9 <xsd:anyAttribute> 元素
使来自指定命名空间的任何属性可以显示在包含 complexType 元素或包含 attributeGroup 元素中。
语法:
<anyAttribute
id = ID
namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local))) : ##any
processContents = (lax | skip | strict): strict
{any attributes with non-schema Namespace…}>
Content: (annotation?)
</anyAttribute>
属性:
属性 | 值 |
---|---|
id |
该元素的 ID。id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。
可选项。 |
namespace |
包含可以使用的属性的命名空间。如果没有指定命名空间,则 ##any 为默认值。如果指定命名空间,则必须是以下值之一。
可选项。 |
processContents |
一个指示器,指示应用程序或 XML 处理器应如何根据由该 anyAttribute 元素指定的属性处理 XML 文档的验证。如果没有指定 processContents 属性,则默认为 strict。如果指定了 processContents,必须是以下值之一。
可选项。 |
元素信息:
出现次数:无限制
父元素:complexType、restriction (simpleContent)、extension (simpleContent)、restriction (complexContent)、extension (complexContent)、attributeGroup
内容:annotation
例子:
以下示例说明一个元素的元素声明,该元素包含一个字符串并且可以具有来自目标命名空间的任何属性。
<xs:element name="stringElementWithAnyAttribute"><xs:complexType><xs:simpleContent><xs:extension base="xs:string"><xs:anyAttribute namespace="##targetNamespace"/></xs:extension></xs:simpleContent></xs:complexType>
</xs:element>
下面的例子是来自名为 “family.xsd” 的 XML schema 的一个片段。它为我们展示了针对 “person” 元素的一个声明。通过使用 元素,我们就可以向 “person” 元素添加任意数量的属性:
<xs:element name="person"><xs:complexType><xs:sequence><xs:element name="firstname" type="xs:string"/><xs:element name="lastname" type="xs:string"/></xs:sequence><xs:anyAttribute/></xs:complexType>
</xs:element>
现在,我们希望通过 “gender” 属性来扩展 “person” 元素。在这种情况下我们就可以这样做,即使这个 schema 的作者从未声明过任何 “gender” 属性。
请看这个 schema 文件,名为 “attribute.xsd”:
<?xml version="1.0" encoding="ISO-8859-1"?>
<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:attribute name="gender"><xs:simpleType><xs:restriction base="xs:string"><xs:pattern value="male|female"/></xs:restriction></xs:simpleType>
</xs:attribute></xs:schema>
下面这个 XML(名为 “Myfamily.xml”),使用了来自不同 schema 的成分,“family.xsd” 和 “attribute.xsd”:
<?xml version="1.0" encoding="ISO-8859-1"?><persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
http://www.w3school.com.cn attribute.xsd"><person gender="female"><firstname>Jane</firstname><lastname>Smith</lastname>
</person><person gender="male"><firstname>David</firstname><lastname>Smith</lastname>
</person></persons>
上面这个 XML 文件是有效的,这是因为 schema “family.xsd” 允许我们向 “person” 元素添加属性。
3. 粒子
之前的例子中,其实已经演示过来这个特性,具有粒子性的元素具有 minOccurs 和 maxOccurs 属性,可以定元素出现的频率
下列元素可以具有 minOccurs 和 maxOccurs 属性。此类元素总是作为复杂类型定义的一部分或命名的模型组的一部分出现。
元素 | 说明 |
---|---|
<xsd:all> 元素 | 允许组中的元素以任意顺序显示(或不显示)在包含元素中。 |
<xsd:any> 元素 | 使来自指定命名空间的任何元素可以显示在包含 sequence 或 choice 元素中。 |
<xsd:choice> 元素 | 允许且仅允许选定组中包含的一个元素出现在包含元素中。 |
<xsd:element> 元素 | 声明一个元素。 |
<xsd:group> 元素 | 将若干元素声明归为一组,以便将它们当作一个组并入复杂类型定义。 |
<xsd:sequence> 元素 | 要求组中的元素以指定的顺序显示在包含元素中。 |
注意:
对于所有的 any、all、choice、sequence、group name 以及 group reference,其中的 maxOccurs 以及 minOccurs 的默认值均为 1。
且all元素的<maxOccurs> 属性只能为1!!!
4. 定义复合元素
学完语法后,我们可以定义复合元素了!!!!
4.1 复合空元素:
空的复合元素不能包含内容,只能含有属性。
一个空的 XML 元素:
<product prodid="1345" />
上面的 “product” 元素根本没有内容。
注意:简易元素,只有文本内容,有属性的元素都属于复合元素。注意区分。
为了定义无内容的类型,我们就必须声明一个在其内容中只能包含属性的类型,并不会声明任何元素,比如这样:
<xs:element name="product"><xs:complexType><xs:complexContent><xs:restriction base="xs:integer"><xs:attribute name="prodid" type="xs:positiveInteger"/></xs:restriction></xs:complexContent></xs:complexType>
</xs:element>
在上面的例子中,我们定义了一个带有复合内容的复合类型。complexContent 元素给出的信号是,我们打算限定或者拓展某个复合类型的内容模型,而 integer 限定则声明了一个属性但不会引入任何的元素内容。
complexContent标签用来重新定义复合类型的内容模型,通过子标签restriction或者extension进行限制或扩展,后面会详细讲。
不过,也可以更加紧凑地声明此 “product” 元素(推荐):
<xs:element name="product"><xs:complexType><xs:attribute name="prodid" type="xs:positiveInteger"/></xs:complexType>
</xs:element>
4.2 复合类型仅包含元素:
“仅含元素”的复合类型元素是只能包含其他元素的元素。
XML 元素,“person”,仅包含其他的元素:
<person><firstname>John</firstname><lastname>Smith</lastname>
</person>
您可在 schema 中这样定义 “person” 元素:
<xs:element name="person"><xs:complexType><xs:sequence><xs:element name="firstname" type="xs:string"/><xs:element name="lastname" type="xs:string"/></xs:sequence></xs:complexType>
</xs:element>
4.3 仅含文本的复合元素
此类型仅包含简易的内容(文本和属性),因此我们要向此内容添加 simpleContent 元素。当使用简易内容时,我们就必须在 simpleContent 元素内定义扩展或限定
这里有一个 XML 元素的例子,“shoesize”,其中仅包含文本:
<shoesize country="france">35</shoesize>
注意:这里也需要和简易元素进行一次比较,加深记忆,简易元素是只含有文本的,只要有属性就是复合元素
下面这个例子声明了一个复合类型,其内容被定义为整数值,并且 “shoesize” 元素含有名为 “country” 的属性:
<xs:element name="shoesize"><xs:complexType><xs:simpleContent><xs:extension base="xs:integer"><xs:attribute name="country" type="xs:string" /></xs:extension></xs:simpleContent></xs:complexType>
</xs:element>
ps:simpleContent和complexContent标签了,看上去和complexType和simpleType很像,到底他们之间有什么关系和区别呢?
simpleContent和complexContent,是在复合类型complexType下使用的,当你希望限定或者拓展某个复合类型的内容模型时使用。
- simpleContent:包含对 complexType 元素(它以字符数据或 simpleType 元素为内容)的扩展或限制并且不包含任何元素。
该复杂类型具有字符数据或将 simpleType 作为内容并且不包含任何元素,但可以包含属性。- complexContent:包含对复杂类型(包含混合内容或仅包含元素)的扩展或限制。
该复杂类型只包含元素或不包含任何元素内容(空)。
4.4 带有混合内容的复合类型
混合的复合类型可包含属性、元素以及文本。
XML 元素,“letter”,含有文本以及其他元素:
<letter>
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>
下面这个 schema 声明了这个 “letter” 元素:
<xs:element name="letter"><xs:complexType mixed="true"><xs:sequence><xs:element name="name" type="xs:string"/><xs:element name="orderid" type="xs:positiveInteger"/><xs:element name="shipdate" type="xs:date"/></xs:sequence></xs:complexType>
</xs:element>
注释:为了使字符数据可以出现在 “letter” 的子元素之间,mixed 属性必须被设置为 “true”。
XML Schema快速入门(三)语法之复杂类型相关推荐
- sql语言和php,SQL语言快速入门(三)_php
我们日常使用SQL语言的工作过程中,使用最多的还是从已经建立好的数据库中查询信息.下面,我们就来详细介绍一下如何使用SQL语言实现各种数据库查询操作. SELECT-FROM 为方便讲解,我们在数据库 ...
- Json Schema快速入门
Json Schema快速入门 JSON 模式是一种基于 JSON 格式定义 JSON 数据结构的规范.它被写在 IETF 草案下并于 2011 年到期.JSON 模式: 描述现有数据格式. 干净的人 ...
- PHP快速入门-基础语法及面向对象
配置sublime {"cmd": ["php", "$file"],"file_regex": "php$& ...
- AS3多线程快速入门(三):NAPE物理引擎+Starling
原文:http://blog.domlib.com/articles/345 [更新]Adobe在11.4正式发布的最后一刻移除了ByteArray.shareable功能的支持,推迟到11.5版本再 ...
- AS3多线程快速入门(三):NAPE物理引擎+Starling[译]
原文链接:http://esdot.ca/site/2012/intro-to-as3-workers-part-3-nape-physics-starling [更新]Adobe在11.4正式发布的 ...
- mybatis快速入门(三)
上面2章写了mybatis的基本操作,今天就写写mybatis的动态代理吧. 动态代理有4个基本原则: 1.userMapper.xml里面的namespace="cn.my.dao.Use ...
- Vue_02 快速入门 基础语法1
目录 1. 模板语法 1.1 插值 1.1.1 文本 1.1.2 html 1.1.3 属性 1.1.4 表达式 1.2 指令 1.2.1 核心指令 2. 过滤器 2.1 局部过滤器 2.2 全局过滤 ...
- Python快速入门--基本语法
5.1 Python简介 本章将介绍Python的最基本语法,以及一些和深度学习还有计算机视觉最相关的基本使用. 5.1.1 Python简史 Python是一门解释型的高级编程语言,特点是简单明确. ...
- HTML5快速入门基础语法
文章目录 前言:一些需要注意的小细节 1.meta标签 2.块元素 3.行内元素 inline element 一.列表 二.HTML 链接 三.结构化语义标签(布局标签) 四.图片标签 五.内联框架 ...
- MySQL简单快速入门 (三)高级查询——JEPLUS软件快速开发平台
03.SQL高级查询_分组: 1).需求:一条查询,查询出每种商品的最高价格 2).分组的命令:group by 分组字段 3).实现上例: select category_id,max(price) ...
最新文章
- AI一分钟 | 小米公布Q2财报,上市以来股价振幅高达30%;俄制造商推出步行杀手机器人...
- js垃圾回收机制和引起内存泄漏的操作
- 常用英文搜索引擎及特点
- CTFshow 信息收集 web5
- sql like 多个值_用于数据分析的8个SQL技术
- python中序列和列表区别细菌真菌病毒_python是哪种动物_动物的分类
- Hive的安装-Mysql安装
- 系统之家win11最新旗舰版64位镜像v2021.07
- Unity开发《一起来捉妖》教程 | 3.随机妖怪位置及旋转提示
- 计算机组成原理浮点数左移规则,2020考研计算机组成原理知识点:浮点数的表示和运算...
- access通过身份证号提取性别_从身份证号码中提取出生年月,性别等都不掌握,那就真的Out了...
- H.264再学习 -- 目前主流的几种数字视频压缩编解码标准(转载)
- 【网络安全工程师面试合集】—社会工程学到底是什么?
- 微信扫描二维码-电脑上网
- 【新闻】李晓翾先生荣获北美产险精算学会2022年度卓越成就奖
- OPT机器视觉12月高峰论坛一览表
- vue3解读—reactivity响应式实现
- 《游戏机图鉴》:一份献给游戏玩家的回忆录
- 自控考研复习 自我梳理(三) 知识来自网络,纯为总结侵权即删(二阶系统)
- 摧毁公司的不是对手,而是价值观
热门文章
- python手机连点器代码_【触动精灵】手机万能连点器 Lua 源码
- Spring Security 原理
- 解决“配置系统未能初始化”问题
- c语言中windows头文件,windows与linux 标准c语言头文件
- 从URL启动程序:也谈谈旺旺的页面启动
- 升级计算机的图形卡和驱动程序,Win10更新显卡驱动程序后无法开机怎么办?解决方案...
- 升余弦滤波器与无码间串扰(二)
- 管理信息系统开发方法——原型法
- fragment中高德地图定位
- 微信小程序_阿里云api人脸识别