XML Schema elementElement


XML Schema 参考手册Complete XML Schema Reference Manual

Definition and Usage

The element element defines an element.

Element Information

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

Syntax

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

(The ? symbol indicates that the element can appear zero or one time within the element element; the * symbol indicates that the element can appear zero or more times.)

Optional. Indicates whether an explicit nil value can be assigned to the element. This applies to the element content and is not an attribute of the element. The default value is false.

If nillable is true, it will allow an instance of this element to set the nil attribute to true. The nil attribute is defined as part of the XML Schema namespace for instances.

Attributes Description
id Optional. Specifies a unique ID for the element.
name Optional. Specifies the name of the element. If the parent element is the schema element, this attribute is required.
ref Optional. A reference to another element. The ref attribute can contain a namespace prefix. This attribute is not used if the parent element is the schema element.
type Optional. Specifies the name of a built-in data type, or the name of a simpleType or complexType element.
substitutionGroup Optional. Specifies the name of an element that can be used to substitute for this element. That element must have the same type or a type derived from the specified element type. This attribute cannot be used if the parent element is not the schema element.
default Optional. Specifies a default value for the element (only used when the element content is a simple type or textOnly).
fixed Optional. Specifies a fixed value for the element (only used when the element content is a simple type or textOnly).
form

Optional. The form of the element. The default value is the value of the elementFormDefault attribute of the schema element containing this attribute. The value must be one of the following strings: "qualified" or "unqualified".

If the parent element is the schema element, this attribute cannot be used.

  • If the value is "unqualified", the element does not need to be qualified with a namespace prefix.
  • If the value is "qualified", the element must be qualified with a namespace prefix.
maxOccurs

Optional. Specifies the maximum number of times the element element can appear in the parent element. The value can be an integer greater than or equal to zero. To set no limit on the maximum number, use the string "unbounded". The default value is 1.

If the parent element is the schema element, this attribute cannot be used.

minOccurs

Optional. Specifies the minimum number of times the element element can appear in the parent element. The value can be an integer greater than or equal to zero. The default value is 1.

If the parent element is the schema element, this attribute cannot be used.

nillable
abstract

Optional. Indicates whether the element can be used in an instance document. If this value is true, the element cannot appear in an instance document. Instead, another element whose substitutionGroup attribute contains the qualified name (QName) of this element must appear in its place. Multiple elements can reference this element in their substitutionGroup attribute. The default value is false.

block

Optional. Derived types. The block attribute prevents elements with the specified derivation type from being used to substitute for this element. The value can contain #all or a list that is a subset of extension, restriction, or substitution:

  • extension - prevents elements derived by extension from being used to substitute for this element.
  • restriction - prevents elements derived by restriction from being used to substitute for this element.
  • substitution - prevents elements derived by substitution from being used to substitute for this element.
  • #all - prevents all derived elements from being used to substitute for this element.
final

Optional. Sets the default value of the final attribute on the element element. This attribute cannot be used if the parent element is not the schema element. The value can contain #all or a list that is a subset of extension or restriction:

  • extension - prevents elements derived by extension from being used to substitute for this element
  • restriction - prevents elements derived by restriction from being used to substitute for this element
  • #all - prevents all derived elements from being used to substitute for this element
any attributes

Optional. Specifies any other attributes with a non-schema namespace.

Example 1

The following example is a schema containing four simple elements: "fname", "lname", "age", and "dateborn", with types 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 with a complex type "note" element. The "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

This example is the same as Example 2, but in this case we choose to use the ref attribute to reference the element names:

<?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 参考手册Complete XML Schema Reference Manual Other Extensions