用指示器(Indicators)我们可以控制文件中元素的使用方法,有七种指示器:
1、顺序指示器:All、Choice、Sequence;
2、出现次数指示器:maxOccurs 、minOccurs;
3、组指示器:Groupname、attributeGroup name;
-
顺序指示器(Indicators)用于指定元素的顺序
<all>指示器(Indicators)指明了子元件可以以任何次序出现,并且每个子元件只能出现一次:1
<
xs:element
name
=
"Author"
>
2
<
xs:complexType
>
3
<
xs:all
>
4
<
xs:element
name
=
"firstname"
type
=
"xs:string"
/>
5
<
xs:element
name
=
"lastname"
type
=
"xs:string"
/>
6
</
xs:all
>
7
</
xs:complexType
>
8
</
xs:element
>
1
<
xs:element
name
=
"Author"
>
2
<
xs:complexType
>
3
<
xs:choice
>
4
<
xs:element
name
=
"firstname"
type
=
"xs:string"
/>
5
<
xs:element
name
=
"lastname"
type
=
"xs:string"
/>
6
</
xs:choice
>
7
</
xs:complexType
>
8
</
xs:element
>
1
<
xs:element
name
=
"Author"
>
2
<
xs:complexType
>
3
<
xs:sequence
>
4
<
xs:element
name
=
"firstname"
type
=
"xs:string"
/>
5
<
xs:element
name
=
"lastname"
type
=
"xs:string"
/>
6
</
xs:sequence
>
7
</
xs:complexType
>
8
</
xs:element
>
-
出现的次数指示器:
最多出现次数指示器(Indicators)指明了一个元素可以出现的最多次数;最少出现次数指示器(Indicators)指明了一个元素要出现的最小次数:1
<
xs:element
name
=
"Author"
>
2
<
xs:complexType
>
3
<
xs:sequence
>
4
<
xs:element
name
=
"firstname"
type
=
"xs:string"
minOccurs
=
"0"
/>
5
<
xs:element
name
=
"lastname"
type
=
"xs:string"
maxOccurs
=
"2"
/>
6
</
xs:sequence
>
7
</
xs:complexType
>
8
</
xs:element
>
-
组指示器(Indicators)用于定义相关的元素组:
1、元素组:你必须在组声明里定义一个all, choice,或sequence元素。下面的例子定义了一个名为"persongroup"的组:属性组声明好以后,我们可以这样引用:1
<
xs:group
name
=
"persongroup"
>
2
<
xs:sequence
>
3
<
xs:element
name
=
"firstname"
/>
4
<
xs:element
name
=
"lastname"
/>
5
</
xs:sequence
>
6
</
xs:group
>
01
<
xs:group
name
=
"persongroup"
>
02
<
xs:sequence
>
03
<
xs:element
name
=
"firstname"
/>
04
<
xs:element
name
=
"lastname"
/>
05
</
xs:sequence
>
06
</
xs:group
>
07
08
<
xs:element
name
=
"person"
type
=
"personinfo"
></
xs:element
>
09
<
xs:complexType
name
=
"personinfo"
>
10
<
xs:sequence
>
11
<
xs:group
ref
=
"persongroup"
/>
12
<
xs:element
name
=
"country"
type
=
"xs:string"
/>
13
</
xs:sequence
>
14
</
xs:complexType
>
1
<
xs:attributeGroup
name
=
"personattrgroup"
>
2
<
xs:attribute
name
=
"firstname"
type
=
"xs:string"
/>
3
<
xs:attribute
name
=
"lastname"
type
=
"xs:string"
/>
4
<
xs:attribute
name
=
"birthday"
type
=
"xs:date"
/>
5
</
xs:attributeGroup
>
01
<
xs:attributeGroup
name
=
"personattrgroup"
>
02
03
<
xs:attribute
name
=
"firstname"
type
=
"xs:string"
/>
04
<
xs:attribute
name
=
"lastname"
type
=
"xs:string"
/>
05
<
xs:attribute
name
=
"birthday"
type
=
"xs:date"
/>
06
07
</
xs:attributeGroup
>
08
<
xs:element
name
=
"person"
>
09
<
xs:complexType
>
10
<
xs:attributeGroup
ref
=
"personattrgroup"
/>
11
</
xs:complexType
>
12
13
</
xs:element
>
其他:
<any>元素可以使我们在XML文档中添加没有被schema 定义过的新元素从而扩充XML文档;
<anyAttribute>元素可使我们在XML文档中添加未被schema指定过的属性;
01 | < xs:element name = "person" > |
02 | < xs:complexType > |
03 | < xs:sequence > |
04 | < xs:element name = "firstname" type = "xs:string" /> |
05 |
06 | < xs:element name = "lastname" type = "xs:string" /> |
07 | < xs:any minOccurs = "0" /> |
08 | </ xs:sequence > |
09 | </ xs:complexType > |
10 |
11 | </ xs:element > |
我们可以在"person"元素的内容里扩充任意元素(在<lastname>的后面);
请看下面名为"children.xsd"的schema文件:
01 | <? xml version = "1.0" encoding = "ISO-8859-1" ?> |
02 |
03 | < xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema" |
04 | targetNamespace = "http://www.w3schools.com" |
05 | xmlns = "http://www.w3schools.com" |
06 | elementFormDefault = "qualified" > |
07 | < xs:element name = "children" > |
08 | < xs:complexType > |
09 |
10 | < xs:sequence > |
11 | < xs:element name = "childname" type = "xs:string" |
12 | maxOccurs = "unbounded" /> |
13 | </ xs:sequence > |
14 |
15 | </ xs:complexType > |
16 | </ xs:element > |
17 | </ xs:schema > |
下面的XML文件(叫做"Myfamily.xml"),用上了来自"family.xsd" 和"children.xsd"两篇不同schema的组件:
01 | <? xml version = "1.0" encoding = "ISO-8859-1" ?> |
02 | < persons xmlns = "http://www.microsoft.com" |
03 |
04 | xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
05 | xsi:SchemaLocation="http://www.microsoft.com family.xsd |
06 | http://www.w3schools.com children.xsd"> |
07 | < person > |
08 | < firstname >Hege</ firstname > |
09 | < lastname >Refsnes</ lastname > |
10 |
11 | < children > |
12 | < childname >Cecilie</ childname > |
13 | </ children > |
14 | </ person > |
15 | < person > |
16 | < firstname >Stale</ firstname > |
17 |
18 | < lastname >Refsnes</ lastname > |
19 | </ person > |
20 | </ persons > |
<any> 和<anyAttribute>元素是用于制造可扩展文档的!它们允许文档含有没有在主要XML schema里声明过的其它新元素。
元素替代:substitutionGroup
1 | < xs:element name = "name" type = "xs:string" /> |
2 | < xs:element name = "navn" substitutionGroup = "name" /> |
3 | < xs:complexType name = "custinfo" > |
4 | < xs:sequence > |
5 | < xs:element ref = "name" /> |
6 | </ xs:sequence > |
7 | </ xs:complexType > |
8 | < xs:element name = "customer" type = "custinfo" /> |
9 | < xs:element name = "kunde" substitutionGroup = "customer" /> |
1 | < customer > |
2 | < name >John Smith</ name > |
3 |
4 | </ customer > |
1 | < kunde > |
2 |
3 | < navn >John Smith</ navn > |
4 | </ kunde > |
关闭替代元素:为了防止其他元素被已指定的元素替代(Element Substitution),可以用block属性;
1 | < xs:element name = "name" type = "xs:string" block = "substitution" /> |
1、可替代元素类型应和标题元素的类型相同,或是从中派生出来的。如果可替代元素类型和标题元素的类型相同,你就不需要再指明可替代元素的类型了;
2、可替代元素组里的所有元素(标题元素和可替代元素)必须声明为“全域元素(global element)”,否则它是不会作用的;
“全域元素”是"schema"元素下面的直接子元素。“本地元素”是嵌套在别的元素里的元素