XML Schema 'element' Element
Definition and Usage
The 'element' element defines an element.
Element Information
Occurrence | The number of elements defined in the schema. |
Parent Element | schema, choice, all, sequence |
Content | simpleType, complexType, key, keyref, unique |
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 declares an element that can appear zero or one times, and the '*' symbol declares an element that can appear zero or more times.)
Attributes
id
Optional. Specifies a unique ID for the element.
name
Optional. Specifies the name of the element. This attribute is required if the parent element is a schema element.
ref
Optional. References another element. The ref attribute can contain a namespace prefix. If the parent element is a schema element, this attribute is not used.
type
Optional. Specifies the name of an intrinsic data type, or specifies 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. The 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 a schema element.
default
Optional. Specifies a default value for the element (to be used only when the element content is of a simple type or textOnly).
fixed
Optional. Specifies a fixed value for the element (to be used only when the element content is of 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 that contains this attribute. This value must be one of the following strings: "qualified" or "unqualified".
This attribute cannot be used if the parent element is a 'schema' element.
- If the value is "unqualified", there is no need to qualify the element by a namespace prefix.
- If the value is "qualified", the element must be qualified by a namespace prefix.
maxOccurs
Optional. Specifies the maximum number of times that the element can appear within its parent element. This value can be an integer greater than or equal to zero. If you do not want to set any limit on the maximum number, please use the string "unbounded". The default value is 1.
This attribute cannot be used if the parent element is a 'schema' element.
minOccurs
Optional. Specifies the minimum number of times the 'element' can appear in the parent element. The value can be an integer greater than or equal to zero. The default value is 1.
This attribute cannot be used if the parent element is a 'schema' element.
nillable
Optional. Indicates whether an explicit zero 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 the instance of the element to set the 'nil' attribute to true. The 'nil' attribute is defined as part of the instance's XML schema namespace.
For example, the next section defines a single element while setting 'nillable' to true.
<xs:element name="myDate" type="xs:date" nillable="true"/>
The next section uses this element and has an explicit zero value (nil attribute set to true).
<myDate xsi:nil="true"></myDate>
abstract
Optional. Indicates whether the element can be used in the instance document. If the value is true, the element cannot appear in the instance document. Conversely, the elements that contain the qualified name (QName) of the element in the 'substitutionGroup' attribute must appear in the position of the element. Multiple elements can refer to the element in their 'substitutionGroup' attribute. The default value is false.
block
Optional. Derived type. The 'block' attribute prevents elements with a specified derived type from being used to substitute for this element. The value can include '#all' or a list, which is a subset of 'extension', 'restriction', or 'substitution':
- extension - Prevents the extension of derived elements from being used to substitute for this element.
- restriction - Prevents the restriction of derived elements from being used to substitute for this element.
- substitution - Prevents the substitution of derived elements 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. This attribute cannot be used if the parent element is not a 'schema' element. The value can include '#all' or a list, which is a subset of 'extension' or 'restriction':
- extension - Prevents the use of extended derived elements to replace this element
- restriction - Prevents the use of restricted derived elements to replace this element
- #all - Prevents all derived elements from being used to replace this element
any attributes
Optional. Specifies any other attributes with non-schema namespaces.
Instance
Example 1
The following example is a schema that contains four simple elements: "fname", "lname", "age", and "dateborn", with types of 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 similar to Example 2, but in this example, we choose to use the 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>