XSD 指示器复合类型

来源:互联网 发布:宿州华为云计算基地 编辑:程序博客网 时间:2024/04/28 19:03

We can control HOW elements are to be used in documents with indicators.
用指示器(Indicators)我们可以控制文件中元素的使用方法(HOW TO USE)。


Indicators
指示器(Indicators)

There are seven indicators:
有7种指示器(Indicators)

Order indicators:
顺序指示器(Indicators)

  • All
    全部
  • Choice
    选择
  • Sequence
    按顺序

Occurrence indicators:
出现次数指示器(Indicators):

  • maxOccurs
    最多出现次数
  • minOccurs
    最少出现次数

Group indicators:
组指示器(Indicators):

  • Group name
    组名
  • attributeGroup name
    属性组名称

Order Indicators
顺序指示器(Indicators)

Order indicators are used to define the order of the elements.
顺序指示器(Indicators)用于指定元素的顺序。

All Indicator
全部指示器(Indicators)

The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once:
<all>指示器(Indicators)指明了子元件可以以任何次序出现,并且每个子元件只能出现一次:

<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>指示器(Indicators)时你可以把<minOccurs>指示器(Indicators)设为0或1,<maxOccurs>指示器(Indicators)只能设为1(<minOccurs> 和 <maxOccurs> 我们后头再说)

Choice Indicator
选择指示器(Indicators)

The <choice> indicator specifies that either one child element or another can occur:
<choice>指示器(Indicators)指明了随便的子元素都可以出现:

<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
有序指示器(Indicators)

The <sequence> indicator specifies that the child elements must appear in a specific order:
<sequence>指示器(Indicators)指定了子元素必须以一个指明的顺序出现:

<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
出现次数指示器(Indicators)

Occurrence indicators are used to define how often an element can occur.
出现次数指示器(Indicators)用于定义一个元素可以出现的次数

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"指示器(Indicators)(any, all, choice, sequence,group name, 和 group reference)来说,maxOccurs 和 minOccurs的默认值都是1

maxOccurs Indicator
最多出现次数指示器(Indicators)

The <maxOccurs> indicator specifies the maximum number of times an element can occur:
最多出现次数指示器(Indicators)指明了一个元素可以出现的最多次数:

<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
最少出现次数指示器(Indicators)

The <minOccurs> indicator specifies the minimum number of times an element can occur:
最少出现次数指示器(Indicators)指明了一个元素要出现的最小次数:

<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"元素里最少出现0次(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
组指示器(Indicators)

Group indicators are used to define related sets of elements.
组指示器(Indicators)用于定义相关的元素组。

Element Groups
元素组

Element groups are defined with the group declaration, like this:
元素组要定义组声明,像这样:

<xs:group name="groupname">  ...</xs:group>

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(属性组)声明,像这样:

<xs:attributeGroup name="groupname">  ...</xs: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>

 
原创粉丝点击