We can control HOW elements are to be used in documents with indicators.
通过使用指示器(Indicators),我们可以控制文件中元素的使用方法。
There are seven indicators:
指示器一共有7种类型,具体如下:
Order indicators:
顺序指示器
- All
全部
- Choice
选择
- Sequence
序列
Occurrence indicators:
频率指示器:
- maxOccurs
{zd0}出现次数
- minOccurs
最小出现次数
Group indicators:
组指示器:
- Group name
组名
- attributeGroup name
属性组名称
Order indicators are used to define the order of the elements.
顺序指示器用于定义元素的排列顺序。
All Indicator
全部指示器
The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once:
<all>指示器指明了子元素可以按照任意次序出现,并且每个子元素只允许出现一次:
<xs:element name="person"> <xs:complexType>
<xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/>
</xs:all> </xs:complexType> </xs:element>
|
Note: When using the <all> indicator you can set the <minOccurs> indicator to 0 or 1 and the <maxOccurs> indicator can only be set to 1 (the <minOccurs> and <maxOccurs> are described later).
注意:在使用<all>指示器时,你可以把<minOccurs>指示器设置为0或1,<maxOccurs>指示器只能设置为1(<minOccurs> 和 <maxOccurs> 的具体内容我们将在后面的部分中作具体阐释)。
Choice Indicator
选择指示器
The <choice> indicator specifies that either one child element or another can occur:
<choice> 指示器指明了任一子元素或其它元素都可以出现:
<xs:element name="person"> <xs:complexType> <xs:choice>
<xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> </xs:choice>
</xs:complexType> </xs:element>
|
Sequence Indicator
序列指示器
The <sequence> indicator specifies that the child elements must appear in a specific order:
<sequence>指示器指定了子元素必须以一个指定的顺序出现:
<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>
|
Occurrence indicators are used to define how often an element can occur.
频率指示器为一个元素指定出现的次数。
Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1.
注意:对于所有的"Order"和"Group"指示器(any、all、choice、sequence、group name 和 group reference)来说,maxOccurs 和 minOccurs的默认值都是1。
maxOccurs Indicator
{zd0}频率指示器
The <maxOccurs> indicator specifies the maximum number of times an element can occur:
<maxOccurs>指示器指定了一个元素可以出现的{zd0}次数:
<xs:element name="person">
<xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10"/>
</xs:sequence> </xs:complexType> </xs:element>
|
The example above indicates that the "child_name" element can occur a minimum of one time (the default value for minOccurs is 1) and a maximum of ten times in the "person" element.
上述案例指定了"child_name"元素在"person"元素里最少必须出现1次(minOccurs的默认值为1),最多可以出现10次。
minOccurs Indicator
最小频率指示器
The <minOccurs> indicator specifies the minimum number of times an element can occur:
<minOccurs> 指示器指定了一个元素必须出现的最小次数:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/> </xs:sequence>
</xs:complexType> </xs:element>
|
The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element.
上述案例指定了"child_name"元素可以不在"person"元素内出现(minOccurs的默认值为1);或者,最多只允许出现10次。
Tip: To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement:
提示:为了使元素可以不限次数地重复出现,可以设置 maxOccurs="unbounded" 。
A working example:
实际案例:
An XML file called "Myfamily.xml":
下面列举了一个名为"Myfamily.xml"的XML文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="family.xsd">
<person> <full_name>Hege Refsnes</full_name> <child_name>Cecilie</child_name> </person>
<person>
<full_name>Tove Refsnes</full_name> <child_name>Hege</child_name> <child_name>Stale</child_name> <child_name>Jim</child_name> <child_name>Borge</child_name>
</person>
<person> <full_name>Stale Refsnes</full_name> </person>
</persons>
|
The XML file above contains a root element named "persons". Inside this root element we have defined three "person" elements. Each "person" element must contain a "full_name" element and it can contain up to five "child_name" elements.
上述XML文件中包含了一个名为"persons"的根元素。在这个根元素中,我们已经定义了3个"person"元素。每个"person"元素必须包含一个"full_name"元素,而且最多只能包含5个"child_name"元素。
Here is the schema file "family.xsd":
下面列举了一份名为"family.xsd"的schema文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="persons"> <xs:complexType> <xs:sequence> <xs:element name="person" maxOccurs="unbounded">
<xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string"
minOccurs="0" maxOccurs="5"/> </xs:sequence> </xs:complexType> </xs:element>
</xs:sequence> </xs:complexType> </xs:element>
</xs:schema>
|
Group indicators are used to define related sets of elements.
组指示器用于定义相关的元素组。
Element Groups
元素组
Element groups are defined with the group declaration, like this:
元素组必须通过组声明进行定义,如下所示:
You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "persongroup", that defines a group of elements that must occur in an exact sequence:
你必须在组声明里定义一个all、choice 或 sequence 元素。下述案例定义了一个名为"persongroup"的组,并要求一组元素必须以指定的顺序出现:
<xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence>
</xs:group>
|
After you have defined a group, you can reference it in another definition, like this:
定义了一个组之后,你可以在另一个组中引用它,如下所示:
<xs:group name="persongroup">
<xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/> </xs:sequence> </xs:group>
<xs:element name="person" type="personinfo"/>
<xs:complexType name="personinfo"> <xs:sequence> <xs:group ref="persongroup"/> <xs:element name="country" type="xs:string"/>
</xs:sequence> </xs:complexType>
|
Attribute Groups
属性组
Attribute groups are defined with the attributeGroup declaration, like this:
属性组必须由attributeGroup(属性组)声明进行定义,如下所示:
The following example defines an attribute group named "personattrgroup":
下述案例定义了名为"personattrgroup"的一个属性组:
<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/> </xs:attributeGroup>
|
After you have defined an attribute group, you can reference it in another definition, like this:
定义了一个属性组之后,你也可以在别的定义中引用它,如下所示:
<xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>
<xs:element name="person"> <xs:complexType> <xs:attributeGroup ref="personattrgroup"/> </xs:complexType>
</xs:element>
|