XML Schema attribute要素
完全なXML Schemaリファレンスマニュアル
定義と使用法
attribute 要素は属性を定義します。
要素情報
- 親要素: attributeGroup, schema, complexType, restriction (both simpleContent and complexContent), extension (both simpleContent and complexContent)
構文
<attribute
default=string
fixed=string
form=qualified|unqualified
id=ID
name=NCName
ref=QName
type=QName
use=optional|prohibited|required
any attributes
>
(annotation?,(simpleType?))
</attribute>
default=string
fixed=string
form=qualified|unqualified
id=ID
name=NCName
ref=QName
type=QName
use=optional|prohibited|required
any attributes
>
(annotation?,(simpleType?))
</attribute>
(? 記号は、該当の要素が attribute 要素内に 0 回または 1 回出現できることを宣言します。)
| 属性 | 説明 |
|---|---|
| default | 任意。属性のデフォルト値を指定します。default 属性と fixed 属性は同時に指定できません。 |
| fixed | 任意。属性の固定値を指定します。default 属性と fixed 属性は同時に指定できません。 |
| form | 任意。属性の形式を指定します。デフォルト値は、その属性を含む schema 要素の attributeFormDefault 属性の値です。次の値を設定できます:
|
| id | 任意。この要素の一意の ID を指定します。 |
| name | 任意。属性の名前を指定します。name 属性と ref 属性は同時に指定できません。 |
| ref | 任意。指定された属性への参照を指定します。name 属性と ref 属性は同時に指定できません。ref が出現する場合、simpleType 要素、form、type は出現できません。 |
| type | 任意。組み込みのデータ型または単純型を指定します。type 属性は、コンテンツに simpleType 要素が含まれない場合にのみ出現できます。 |
| use | 任意。属性の使用方法を指定します。次の値を設定できます:
|
| any attributes | 任意。non-schema 名前空間を持つその他の属性を指定します。 |
例 1
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
上記の例は、"code" 属性に制限があることを示しています。唯一受け入れ可能な値は、大文字の A から Z までの 2 文字です。
例 2
複合型で既存の属性定義を使用して属性を宣言するには、ref 属性を使用します:
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:complexType name="someComplexType">
<xs:attribute ref="code"/>
</xs:complexType>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:complexType name="someComplexType">
<xs:attribute ref="code"/>
</xs:complexType>
例 3
属性はデフォルト値を持つことも、指定された固定値を持つこともできます。他の値が指定されていない場合、デフォルト値が自動的に属性に割り当てられます。次の例では、デフォルト値は "EN" です:
<xs:attribute name="lang" type="xs:string" default="EN"/>
他の値が指定されていない場合、固定値が自動的に属性に割り当てられます。ただし、デフォルト値とは異なり、固定値以外の値を属性に指定すると、ドキュメントは無効として検証されます。次の例では、固定値は "EN" です:
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
例 4
すべての属性はデフォルトでオプションです。属性を明示的にオプションとして指定するには、"use" 属性を使用します:
<xs:attribute name="lang" type="xs:string" use="optional"/>
属性を必須にするには:
<xs:attribute name="lang" type="xs:string" use="required"/>
完全なXML Schemaリファレンスマニュアル その他の拡張