XML Schema 2章 Complex Type
- 2.1 XML Schema サンプル
- 2.2 要素の宣言
- 2.3 Complex Typeの定義
- 2.4 子供要素
- 2.5 sequence, choice, all
- 2.6 minOccursとmaxOccurs
- 2.7 モデルグループ
- 2.8 いろいろなComplex Type
- 2.9 まとめ
2.8 いろいろなComplex Type
上で説明したコンポーネントを組み合わせることにより、いろいろなComplex Typeを定義することができます。
以下に例を示します。現在、purchaseOrderでは、購入した品物の送付先住所と支払いの請求先住所を別々の要素で定義しています。もし送付先住所と請求先住所が等しい場合は、singleUSAddressという要素の中にその住所を登録する、という形に変更します。スキーマは以下のようになります。
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:choice>
<xsd:group ref="po:shipAndBill"/>
<xsd:element name="singleUSAddress" type="po:USAddress"/>
</xsd:choice>
<xsd:element ref="po:comment" minOccurs="0"/>
<xsd:element name="items" type="Items"/>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
<xsd:group name="shipAndBill">
<xsd:sequence>
<xsd:element name="shipTo" type="po:USAddress"/>
<xsd:element name="billTo" type="po:USAddress"/>
</xsd:sequence>
</xsd:group>
上の例では、sequenceの要素の中で、choice 要素を使用しています。さらに、 choiceの内容では、 singleUSAddress要素と、shipAndBillというモデルグループを指定しています。shipAndBillは、group要素で定義されています。どのようなグループかというと、shipTo要素とbillTo要素が順番に出現するものです。以下のXML文書はすべて、スキーマに沿った文書になります。
<?xml version="1.0"?>
<purchaseOrder orderDate="1999-10-20">
<singleUSAddress country="US">
<name>Alice Smith</name>
<street>123 Maple Street</street>
<city>Mill Valley</city>
<state>CA</state>
<zip>90952</zip>
</singleUSAddress>
<items>
<item partNum="872-AA">
<productName>Lawnmower</productName>
<quantity>1</quantity>
<USPrice>148.95</USPrice>
<comment>Confirm this is electric</comment>
</item>
</items>
</purchaseOrder>
また、sequence要素や、choice要素にminOccursやmaxOccursを指定することがきます。例えば、以下のように記述すれば、「emph要素とstrong要素が任意の回数、任意の順番で出現する」という意味になります。
<xs:complexType name="paraType" mixed="true"> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element ref="po:emph"/> <xs:element ref="po:strong"/> </xs:choice> <xs:attribute name="version" type="xs:number"/> </xs:complexType>
Complex Typeでは、以下の指定ができます。
- sequence、choice要素にminOccurs、maxOccurs属性を指定する
- sequence、choice要素の中にgroupを含める
- sequence、choice要素の中に、さらにsequence、choice要素を含める
all要素は、SGMLのDTDにあった機能を継承したものになります。使用には、以下のような制限があります。
- all要素は、complexType 要素の直下の子供要素にしか存在できません。 例えば、以下のスキーマは、allがselect要素の中に出現しているため、エラーになります。
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:all>
<xsd:element name="shipTo" type="USAddress"/>
<xsd:element name="billTo" type="USAddress"/>
<xsd:element name="items" type="Items"/>
</xsd:all>
<xsd:sequence>
<xsd:element ref="po:comment" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
- all要素の子供要素として、グループを指定することはできません。elementである必要があります。例えば、以下のスキーマはall要素の中で、グループを指定しているためエラーになります。
<xsd:complexType name="PurchaseOrderType">
<xsd:all>
<xsd:group ref="po:shipAndBill"/>
<xsd:element name="items" type="Items"/>
</xsd:all>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
- all要素のminOccursは0か1、maxOccursは1しか設定できません。例えば、先ほど、all要素の例として、以下のようなものを挙げました。
<xsd:complexType name="PurchaseOrderType"> <xsd:all> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> <xsd:element ref="po:comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:all> <xsd:attribute name="orderDate" type="xsd:date"/> </xsd:complexType>
以上の例では、comment要素は、どこに出現してもいいですが、複数回出現することはできません。上を変更するために、maxOccurs属性をunboundedに設定するようなことはできません。
2.9 まとめ
- 要素の型にはSimple TypeとComplex Typeがある。
- Complex Typeは子供要素を持つ要素の型
- 出現の順番を指定するsequence、choice、all要素
- 出現回数を指定するminOccurs、maxOccurs属性

