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))
jede Eigenschaft
>
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. References another element. The ref attribute can contain a namespace prefix. This attribute is not used if the parent element is a schema element.

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 this 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 a simple type or textOnly).

fixed

Optional. Specifies a fixed value for the element (to be used only 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 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", the element does not need to be qualified 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 the element can appear within its parent element. This value must 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 XML schema namespace of the instance.

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 at the location of the element. Multiple elements can reference 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 this element. The value can include #all or a list, which is a subset of extension, restriction, or substitution:

  • extension - Prevent derived elements from being used to replace this element through extension.
  • restriction - Prevent derived elements from being used to replace this element through restriction.
  • substitution - Prevent derived elements from being used to replace this element through substitution.
  • #all - Prevent all derived elements from being used to replace this element.

final

Optional. Set 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 - Verhindert, dass durch Erweiterungen abgeleitete Elemente dieses Elements ersetzt werden können
  • restriction - Verhindert, dass durch Beschränkungen abgeleitete Elemente dieses Elements ersetzt werden können
  • #all - Verhindert, dass alle abgeleiteten Elemente dieses Elements ersetzen können

jede Eigenschaft

Optional. Definiert alle anderen Attribute mit non-schema Namensräumen.

Beispiel

Beispiel 1

Das folgende Beispiel ist ein Schema, das vier einfache Elemente enthält: "fname", "lname", "age" und "dateborn", deren Typen string, nonNegativeInteger und date sind:

<?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:element name="body" type="xs:string"/>

Beispiel 2

Das folgende Beispiel ist ein Schema mit einem komplexen Typ "note"-Element. Das "note"-Element enthält vier einfache Elemente: "to", "from", "heading" und "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:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="body" type="xs:string"/>

Beispiel 3

Dieser Beispiel ist dem Beispiel 2 ähnlich, aber in diesem Beispiel wählen wir die Verwendung der ref-Attribut, um auf den Elementnamen zu verweisen:

<?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"/>