2010年3月25日木曜日

[XML] やりなおし XML Schema (5)

やりなおし XML Schema (4) の続き。
内容としては、やりなおし XML Schema (3) の続き。

名前をもたない型 (anonymous type)

XML Schema では

(1)型(タイプ)を定義

<xsd:complexType name="FooType">
</xsd:complexType>

しておいて、

(2)要素宣言のtype属性で定義した型を指定

<xsd:element name="fooElem" type="FooType"/>

という形が基本形。

でも特定の要素宣言内で1度しか使われない型などは、分けるとかえって面倒だったりわかりづらくなったりする。
そういう場合は、要素宣言の中で名前をもたない型を定義して使うことができる。

名前をもたない型の例。


<xsd:complexType name="Items">
<xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:element name="productName" type="xsd:string"/>
<xsd:element name="quantity">
<xsd:simpleType base="xsd:positiveInteger">
<xsd:maxExclusive value="100"/>
</xsd:simpleType>
</xsd:element>
<xsd:element name="price" type="xsd:decimal"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="shipDate" type="xsd:date" minOccurs='0'/>
<xsd:attribute name="partNum" type="Sku"/>
</xsd:complexType>
</xsd:element>
</xsd:complexType>


この中では、item 要素とquantity要素で名前をもたない型の定義を含んでいる。
どちらもtype属性をもっていない。また、要素宣言内に出てくる型定義(complexType, simpleType)はname属性をもっていない。

つまり、ざっくり肝だけ取り出すと、名前をもたない型の定義方法は以下のようになる。

(複合型)

<xsd:element name="foo">
<xsd:complexType>
... 型定義
</xsd:complexType>
</xsd:element>


(単純型)

<xsd:element name="foo">
<xsd:simpleType>
... 型定義
</xsd:complexType>
</xsd:element>

0 件のコメント:

コメントを投稿