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 indicates that the element can appear zero or one times, and the * symbol indicates that the element 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. Refers to another element. The ref attribute can include 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 as a substitute for the current element. This element must have the same type or be 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 simple type or textOnly).
fixed
Optional. Specifies a fixed value for the element (to be used only when the element content is of 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. The value must be one of the following strings: "qualified" or "unqualified".
This attribute cannot be used if the parent element is the schema element.
- 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 can appear in the parent element. The value can be an integer greater than or equal to zero. If no limit on the maximum number is set, use the string "unbounded". The default value is 1.
This attribute cannot be used if the parent element is the 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 the 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 following section defines a single element while setting nillable to true.
<xs:element name="myDate" type="xs:date" nillable="true"/>
The following 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 substitutionGroup attribute contains other elements with the qualified name (QName) of the element that 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 replace the element. The value can contain #all or a list, which is a subset of extension, restriction, or substitution:
- extension - Prevent elements derived from extensions from being used to replace the element.
- restriction - Prevents derived elements from replacing this element through restriction
- substitution - Prevents derived elements from replacing this element through substitution
- #all - Prevents all derived elements from replacing this element.
final
Optional. Sets the default value of the final attribute on the element. If the parent element is not a schema element, this attribute cannot be used. The value can contain #all or a list, which is a subset of extension or restriction:
- extension - Prevents derived elements from replacing this element through extension
- restriction - Prevents derived elements from replacing this element through restriction
- #all - Prevents all derived elements from replacing 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 the same as Example 2, but in this example, we choose to use the ref attribute to reference 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>