XML Schema attributeGroup 元素
定义和用法
attributeGroup 元素用于对属性声明进行组合,这样这些声明就能够以组合的形式合并到复杂类型中。
元素信息
出现次数 | 无限制 |
父元素 | attributeGroup、complexType、schema、restriction (simpleContent)、extension (simpleContent)、restriction (complexContent)、extension (complexContent) |
内容 | annotation、attribute、attributeGroup、anyAttribute |
语法
<attributeGroup
id=ID
name=NCName
ref=QName
any attributes
>
(annotation?),((attribute|attributeGroup)*,anyAttribute?))
</attributeGroup>
(? 符号声明在 attributeGroup 元素中,元素可出现零次或一次,* 符号声明元素可出现零次或多次。)
属性 | 描述 |
id | 可选。规定该元素的唯一的 ID。 |
name | 可选。规定属性组的名称。name 和 ref 属性不能同时出现。 |
ref | 可选。规定对指定的属性组的引用。name 和 ref 属性不能同时出现。 |
any attributes | 可选。规定带有 non-schema 命名空间的任何其他属性。 |
实例
<xs:attributeGroup name="personattr">
<xs:attribute name="attr1" type="string"/>
<xs:attribute name="attr2" type="integer"/>
</xs:attributeGroup>
<xs:complexType name="person">
<xs:attributeGroup ref="personattr"/>
</xs:complexType>
上面的例子定义了一个名为 "personattr" 的属性组,在名为 "person" 的复杂类型中使用。
XML Schema choice 元素
定义和用法
XML Schema 的 choice 元素仅允许包含在 <choice> 声明中的元素之一出现在包含元素中。
元素信息
出现次数 | 在 group 和 complexType 元素中为一次;其他为无限制。 |
父元素 | group、choice、sequence、complexType、restriction (simpleContent)、extension (simpleContent)、restriction (complexContent)、extension (complexContent) |
内容 | annotation、any、choice、element、group、sequence |
语法
<choice
id=ID
maxOccurs=nonNegativeInteger|unbounded
minOccurs=nonNegativeInteger
any attributes
>
(annotation?,(element|group|choice|sequence|any)*)
</choice>
(? 符号声明在 choice 元素中,元素可出现零次或一次,* 符号声明元素可出现零次或多次。)
属性 | 描述 |
id | 可选。规定该元素的唯一的 ID。 |
maxOccurs | 可选。规定 choice 元素在父元素中可出现的最大次数。该值可以是大于或等于零的整数。若不想对最大次数设置任何限制,请使用字符串 "unbounded"。 默认值为 1。 |
minOccurs | 可选。规定 choice 元素在父元素中可出现的最小次数。该值可以是大于或等于零的整数。若要指定该 any 组是可选的,请将此属性设置为零。 默认值为 1。 |
any attributes | 可选。规定带有 non-schema 命名空间的任何其他属性。 |
实例
<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>
上面的例子定义 "person" 元素必须包含一个 "employee" 元素或一个 "member" 元素。
XML Schem定义和用法
complexContent 元素定义对复杂类型(包含混合内容或仅包含元素)的扩展或限制。
元素信息
出现次数 | 一次 |
父元素 | complexType |
内容 | 可选项。annotation 必选项。 有并且只有一个下列元素: restriction (complexContent) 或 extension (complexContent)。 |
语法
<complexContent
id=ID
mixed=true|falseany attributes
>
(annotation?,(restriction|extension))
</complexContent>
(? 符号声明元素可在 complexContent 元素内出现零次或一次。)
属性 | 描述 |
id | 可选。规定该元素的唯一的 ID。 |
mixed | 可选。规定是否允许字符数据出现在该 complexType 元素的子元素之间。 默认值为 false。 |
any attributes | 可选。规定带有 non-schema 命名空间的任何其他属性。 |
实例
下面的例子中有一个复杂类型 "fullpersoninfo",这个复杂类型是通过用三个补充的元素扩展继承的类型,从另一个复杂类型 "personinfo" 衍生而来的:
<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
在上例中,"employee" 元素必须按顺序包含下面的元素:"firstname"、"lastname"、"address"、"city" 以及 "country"。
XML Schema complexType 元素
定义和用法
complexType 元素定义复杂类型。复杂类型的元素是包含其他元素和/或属性的 XML 元素。
元素信息
出现次数 | 在架构内为无限制;在元素内为一次。 |
父元素 | element、redefine、schema |
内容 | annotation、simpleContent、complexContent、group、all、choice、sequence、attribute、attributeGroup、anyAttribute |
语法
<complexType
id=ID
name=NCName
abstract=true|false
mixed=true|false
block=(#all|list of (extension|restriction))
final=(#all|list of (extension|restriction))
any attributes
>
(annotation?,(simpleContent|complexContent|((group|all|
choice|sequence)?,((attribute|attributeGroup)*,anyAttribute?))))
</complexType>
(? 符号声明在 complexType 元素中,元素可出现零次或一次,* 符号声明元素可出现零次或多次。)
属性
id
可选。规定该元素的唯一的 ID。
name
可选。规定元素的名称。
abstract
可选。规定在实例文档中是否可以使用复杂类型。如果该值为 true,则元素不能直接使用该复杂类型,而是必须使用从该复杂类型派生的复杂类型。 默认值为 false。
mixed
可选。规定是否允许字符数据出现在该复杂类型的子元素之间。 默认值为 false。
· 如果 simpleContent 元素是子元素,则不允许 mixed 属性。
· 如果 complexContent 元素是子元素,则该 mixed 属性可被 complexContent 元素的 mixed 属性重写。
block
可选。防止具有指定派生类型的复杂类型被用来替代该复杂类型。该值可以包含 #all 或者一个列表,该列表是 extension 或 restriction 的子集:
· extension - 防止通过扩展派生的复杂类型被用来替代该复杂类型。
· restriction - 防止通过限制派生的复杂类型被用来替代该复杂类型。
· #all - 防止所有派生的复杂类型被用来替代该复杂类型。
final
可选。防止从该 complexType 元素派生指定的类型。该值可以包含 #all 或者一个列表,该列表是 extension 或 restriction 的子集。
· extension - 防止通过扩展派生。
· restriction - 防止通过限制派生。
· #all - 防止所有派生(扩展和限制)。
any attributes
可选。规定带有 non-schema 命名空间的任何其他属性。
实例
例子 1
下面的例子拥有一个名为 "note" 的复杂类型元素:
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
例子 2
下面的例子中有一个复杂类型 "fullpersoninfo",它通过使用三个补充的元素 (address、city 和 country) 对继承的类型进行扩展,由另一个复杂类型 "personinfo" 派生而来:
<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
在上面的例子中,上面的 "employee" 元素必须按顺序包含下列元素:"firstname", "lastname", "address", "city" 以及 "country"。
XML Schem定义和用法
documentation 元素在 schema 中输入文本注释。该元素必须位于 annotation 元素内。
元素信息
出现次数 | 无限制 |
父元素 | annotation |
内容 | 任何格式正确的 XML 内容。 |
语法
<documentation
source=URI reference
xml:lang=language
>
Any well-formed XML content
</documentation>
属性 | 描述 |
source | 可选。规定应用程序信息的来源。 |
xml:lang | 可选。规定内容中使用的语言。 |
实例
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:annotation>
<xs:appInfo>W3School Note</xs:appInfo>
<xs:documentation xml:lang="en">
This Schema defines a W3School note!
</xs:documentation>
</xs:annotation>
.
.
.
</xs:schema>
XML Schema element 元素
定义和用法
element 元素定义一个元素。
元素信息
出现次数 | 在架构中定义的元素的数目。 |
父元素 | schema、choice、all、sequence |
内容 | simpleType、complexType、key、keyref、unique |
语法
<element
id=ID
name=NCName
ref=QName
type=QName
substitutionGroup=QName
default=string
fixed=string
form=qualified|unqualified
maxOccurs=nonNegativeInteger|unbounded
minOccurs=nonNegativeInteger
nillable=true|false
abstract=true|false
block=(#all|list of (extension|restriction))
final=(#all|list of (extension|restriction))
any attributes
>
annotation?,((simpleType|complexType)?,(unique|key|keyref)*))
</element>
(? 符号声明在 element 元素中,该元素可出现零次或一次,* 符号声明元素可出现零次或多次。)
属性
id
可选。规定该元素的唯一的 ID。
name
可选。规定元素的名称。如果父元素是 schema 元素,则此属性是必需的。
ref
可选。对另一个元素的引用。ref 属性可包含一个命名空间前缀。如果父元素是 schema 元素,则不是使用该属性。
type
可选。规定内建数据类型的名称,或者规定 simpleType 或 complexType 元素的名称。
substitutionGroup
可选。规定可用来替代该元素的元素的名称。 该元素必须具有相同的类型或从指定元素类型派生的类型。
如果父元素不是 schema 元素,则不可以使用该属性。
default
可选。为元素规定默认值(仅当元素内容是简单类型或 textOnly 时使用)。
fixed
可选。为元素规定固定值(仅当元素内容是简单类型或 textOnly 时使用)。
form
可选。该元素的形式。 默认值是包含该属性的 schema 元素的 elementFormDefault 属性的值。 该值必须是下列字符串之一: “qualified” 或 “unqualified”。
如果父元素是 schema 元素,则不能使用该属性。
· 如果该值是 “unqualified”,则无须通过命名空间前缀限定该元素。
· 如果该值是 “qualified”,则必须通过命名空间前缀限定该元素。
maxOccurs
可选。规定 element 元素在父元素中可出现的最大次数。该值可以是大于或等于零的整数。若不想对最大次数设置任何限制,请使用字符串 "unbounded"。 默认值为 1。
如果父元素是 schema 元素,则不能使用该属性。
minOccurs
可选。规定 element 元素在父元素中可出现的最小次数。该值可以是大于或等于零的整数。默认值为 1。
如果父元素是 schema 元素,则不能使用该属性。
nillable
可选。指示是否可以将显式的零值分配给该元素。此项应用于元素内容并且不是该元素的属性。 默认值为 false。
如果 nillable 为 true,将使该元素的实例可以将 nil 属性设置为 true。 nil 属性被定义为实例的 XML 架构命名空间的一部分。
例如,下段定义了单个元素,同时将 nillable 设置为 true。
<xs:element name="myDate" type="xs:date" nillable="true"/>
下段使用该元素并具有显式零值(nil 属性设置为 true)。
<myDate xsi:nil="true"></myDate>
abstract
可选。指示元素是否可以在实例文档中使用。如果该值为 true,则元素不能出现在实例文档中。 相反,substitutionGroup 属性包含该元素的限定名 (QName) 的其他元素必须出现在该元素的位置。多个元素可以在其 substitutionGroup 属性中引用该元素。默认值是 false。
block
可选。派生的类型。 block 属性防止具有指定派生类型的元素被用于替代该元素。该值可以包含 #all 或者一个列表,该列表是 extension、restriction 或 substitution 的子集:
· extension - 防止通过扩展派生的元素被用来替代该元素。
· restriction - 防止通过限制派生的元素被用来替代该元素。
· substitution - 防止通过替换派生的元素被用来替代该元素。
· #all - 防止所有派生的元素被用来替代该元素。
final
可选。设置 element 元素上 final 属性的默认值。如果父元素不是 schema 元素,则不能使用该属性。该值可以包含 #all 或者一个列表,该列表是 extension 或 restriction 的子集:
· extension - 防止通过扩展派生的元素被用来替代该元素
· restriction - 防止通过限制派生的元素被用来替代该元素
· #all - 防止所有派生的元素被用来替代该元素
any attributes
可选。规定带有 non-schema 命名空间的任何其他属性。
实例
例子 1
下面的例子是一个schema,其中带有四个简单元素:"fname", "lname", "age" 以及 "dateborn",类型是 string、nonNegativeInteger 以及 date:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="fname" type="xs:string"/>
<xs:element name="lname" type="xs:string"/>
<xs:element name="age" type="xs:nonNegativeInteger"/>
<xs:element name="dateborn" type="xs:date"/>
</xs:schema>
例子 2
下面的例子是一个带有复杂类型 "note" 元素的 schema。"note" 元素包含四个简单元素:"to", "from", "heading" 以及 "body":
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
例子 3
本例与例子 2 相同,但是在此例中,我们选择使用 ref 属性来引用元素名称:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element ref="to"/>
<xs:element ref="from"/>
<xs:element ref="heading"/>
<xs:element ref="body"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:schema>
XML Schema extension 元素
定义和用法
extension 元素对 simpleType 或 complexType 的元素进行扩展。
元素信息
出现次数 | 一次 |
父元素 | complexContent |
内容 | annotation、attribute、attributeGroup、anyAttribute、choice、all、sequence、group |
语法
<extension
id=ID
base=QName
any attributes
>
(annotation?,((group|all|choice|sequence)?,
((attribute|attributeGroup)*,anyAttribute?)))
</extension>
属性 | 描述 |
id | 可选。规定该元素的唯一的 ID。 |
base | 必需。规定内建数据类型、simpleType 或 complexType 元素的名称。 |
xml:lang | 可选。规定内容中使用的语言。 |
(? 符号声明在 extension 元素内,该元素只能出现零次或一次,* 符号声明该元素可出现零次或多次。)
实例
例子 1
下面的例子通过添加属性,对一个已有的 simpleType 进行扩展:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="size">
<xs:restriction base="xs:string">
<xs:enumeration value="small" />
<xs:enumeration value="medium" />
<xs:enumeration value="large" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="jeans">
<xs:simpleContent>
<xs:extension base="size">
<xs:attribute name="sex">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="male" />
<xs:enumeration value="female" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
例子 2
下面的例子通过添加三个元素,对一个已有的 complexType 元素进行扩展:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
XML Schema field 元素
定义和用法
field 元素规定 XPath 表达式,该表达式指定用来定义标识约束(unique、key 和 keyref 元素)的值(或其中一个值)。
元素信息
出现次数 | 一次 |
父元素 | key、keyref、unique |
内容 | annotation |
语法
<field
id=ID
xpath=XPath expression
any attributes
>
(annotation?)
</field>
属性 | 描述 |
id | 可选。规定该元素的唯一的 ID。 |
xpath | 必需。标识其内容或值用于约束的单个元素或属性。如果该表达式标识一个元素,则该元素必须是简单类型。 |
xml:lang | 可选。规定内容中使用的语言。 |
(? 符号声明在 field 元素内,该元素可出现零次或一次。)
实例
例子 1
下面的例子展示了一个 field 元素,该元素把 "userID" 属性定义为用于标识约束的字段:
<xs:field xpath="@userID"/>
XML Schema group 元素
定义和用法
group 元素用于定义在复杂类型定义中使用的元素组。
元素信息
出现次数 | 无限制 |
父元素 | schema、choice、sequence、complexType、restriction (complexContent)、extension (complexContent) |
内容 | annotation、all、choice、sequence |
语法
<group
id=ID
name=NCName
ref=QName
maxOccurs=nonNegativeInteger|unbounded
minOccurs=nonNegativeInteger
any attributes
>
annotation?,(all|choice|sequence)?)
</group>
(? 符号声明在 group 元素中,该元素可出现零次或一次。)
属性
id
可选。规定该元素的唯一的 ID。
name
可选。规定组的名称。该名称必须是在 XML 命名空间规范中定义的无冒号名称 (NCName)。
仅当 schema 元素是该 group 元素的父元素时才使用该属性。在此情况下,group 是由 complexType、choice 和 sequence 元素使用的模型组。
name 属性和 ref 属性不能同时出现。
ref
可选。引用另一个组的名称。ref 值必须是 QName。 ref 可以包含命名空间前缀。
name 属性和 ref 属性不能同时出现。
maxOccurs
可选。规定 group 元素可在父元素中出现的最大次数。该值可以是大于或等于零的整数。若不想对最大次数设置任何限制,请使用字符串 "unbounded"。默认值为 1。
minOccurs
可选。规定 group 元素可在父元素中出现的最小次数。该值可以是大于或等于零的整数。默认值为 1。
any attributes
可选。规定带有 non-schema 命名空间的任何其他属性。
实例
例子 1
下面的例子定义一个包含四个元素的序列的组,并在一个复杂类型定义中使用了这个 group 元素:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:group name="custGroup">
<xs:sequence>
<xs:element name="customer" type="xs:string"/>
<xs:element name="orderdetails" type="xs:string"/>
<xs:element name="billto" type="xs:string"/>
<xs:element name="shipto" type="xs:string"/>
</xs:sequence>
</xs:group>
<xs:element name="order" type="ordertype"/>
<xs:complexType name="ordertype">
<xs:group ref="custGroup"/>
<xs:attribute name="status" type="xs:string"/>
</xs:complexType>
</xs:schema>