Latest web development tutorials

XML Schema element element

XML Schema Reference Complete XML Schema Reference

Definition and Usage

element element defines an element.

Element Information

  • Parent element: schema, choice, all, sequence , group

grammar

<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>

(? Sign declares that the element element, the element can occur zero or one time, * sign declares that the element can occur zero or more times.)

可选。指示是否可以将显式的零值分配给该元素。此项应用于元素内容并且不是该元素的属性。 默认值为 false。

If nillable is true, it will make an instance of the element can be nil attribute set to true. Part nil attribute is defined as an instance of the XML schema namespace.

属性 描述
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
abstract

Optional. It indicates whether element can be used in the instance document. If the value is true, the element can not appear in the instance document. Instead, substitutionGroup attribute contains the qualified name (QName) of another element of the element must appear in the position of the element. Several elements can refer to it in its substitutionGroup property. The default value is false.

block

Optional. Derived type. block element with the specified attribute to prevent derived types are used in place of the element. This value can contain #all or a list that is extension, restriction or substitution subset:

  • extension - prevents elements derived by extension is used to replace the element.
  • restriction - prevents elements derived by restriction is used to replace the element.
  • substitution - prevents elements derived by replacement is used to replace the element.
  • #all - prevents all derived element is used to replace the element.
final

Optional. Set default values ​​for properties on the final element element. If the parent element is not the schema element, you can not use this property. This value can contain #all or a list that is a subset of extension or restriction:

  • extension - prevents elements derived by extension be used instead of the element
  • restriction - prevents elements derived by restriction is used to replace the element
  • #all - prevents all derived element is used to replace the element
any attributes

Optional. It specifies any other attributes non-schema namespace.

Example 1

The following example is a schema, which has four simple elements: "fname", "lname", "age" and "dateborn", the type is string, nonNegativeInteger and 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>

Example 2

The following example is a schema complex type "note" with the element. "Note" element contains four simple elements: "to", "from", "heading" and "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>

Example 3

In this case the same as in Example 2, but in this case, we chose to use ref attribute to refer to the element name:

<?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 Reference Complete XML Schema Reference