当前位置: 首页 > 网络学院 > XML相关教程 > Schema (XSD) > XSD 指示器复合类型
We can control HOW elements are to be used in documents with indicators.
通过使用指示器(Indicators),我们可以控制文件中元素的使用方法。
There are seven indicators:
指示器一共有7种类型,具体如下:
Order indicators:
顺序指示器
Occurrence indicators:
频率指示器:
Group indicators:
组指示器:
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"> |
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"> |
Sequence Indicator
序列指示器
The <sequence> indicator specifies that the child elements must appear in a specific order:
<sequence>指示器指定了子元素必须以一个指定的顺序出现:
<xs:element name="person"> |
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
最大频率指示器
The <maxOccurs> indicator specifies the maximum number of times an element can occur:
<maxOccurs>指示器指定了一个元素可以出现的最大次数:
<xs:element name="person"> |
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"> |
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" <person> <person> <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:element name="persons"> </xs:schema> |
Group indicators are used to define related sets of elements.
组指示器用于定义相关的元素组。
Element Groups
元素组
Element groups are defined with the group declaration, like this:
元素组必须通过组声明进行定义,如下所示:
<xs:group name="groupname"> |
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"> |
After you have defined a group, you can reference it in another definition, like this:
定义了一个组之后,你可以在另一个组中引用它,如下所示:
<xs:group name="persongroup"> <xs:element name="person" type="personinfo"/> <xs:complexType name="personinfo"> |
Attribute Groups
属性组
Attribute groups are defined with the attributeGroup declaration, like this:
属性组必须由attributeGroup(属性组)声明进行定义,如下所示:
<xs:attributeGroup name="groupname"> |
The following example defines an attribute group named "personattrgroup":
下述案例定义了名为"personattrgroup"的一个属性组:
<xs:attributeGroup name="personattrgroup"> |
After you have defined an attribute group, you can reference it in another definition, like this:
定义了一个属性组之后,你也可以在别的定义中引用它,如下所示:
<xs:attributeGroup name="personattrgroup"> <xs:element name="person"> |