XMLスキーマ属性要素
❮完全なXMLスキーマリファレンス
定義と使用法
属性要素は属性を定義します。
要素情報
- 親要素: attributeGroup、schema、complexType、restriction(simpleContentとcomplexContentの両方)、extension(simpleContentと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>
(?記号は、要素が属性要素内で0回または1回発生する可能性があることを宣言します)
Attribute | Description |
---|---|
default | Optional. Specifies a default value for the attribute. Default and fixed attributes cannot both be present |
fixed | Optional. Specifies a fixed value for the attribute. Default and fixed attributes cannot both be present |
form | Optional. Specifies the form for the attribute. The default value is
the value of the attributeFormDefault attribute of the element
containing the attribute. Can be set to one of the following:
|
id | Optional. Specifies a unique ID for the element |
name | Optional. Specifies the name of the attribute. Name and ref attributes cannot both be present |
ref | Optional. Specifies a reference to a named attribute. Name and ref attributes cannot both be present. If ref is present, simpleType element, form, and type cannot be present |
type | Optional. Specifies a built-in data type or a simple type. The type attribute can only be present when the content does not contain a simpleType element |
use | Optional. Specifies how the attribute is used. Can be one
of the following values:
|
any attributes | Optional. Specifies any other attributes with non-schema namespace |
例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>
上記の例は、「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>
例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スキーマリファレンス