XSD インスタンスの一例
このセクションでは、XML Schemaの作成方法を説明します。schemaの書き方の異なる方法についても学びます。
XMLドキュメント
「shiporder.xml」という名前のXMLドキュメントを見てみましょう:
<?xml version="1.0" encoding="ISO-8859-1"?> <shiporder orderid="889923"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd"> <orderperson>George Bush</orderperson> <shipto> <name>John Adams</name> <address>Oxford Street</address> <city>London</city> <country>UK</country> </shipto> <item> <title>Empire Burlesque</title> <note>Special Edition</note> <quantity>1</quantity> <price>10.90</price> </item> <item> <title>Hide your heart</title> <quantity>1</quantity> <price>9.90</price> </item> </shiporder>
上記のXMLドキュメントには、"shiporder"というルート要素が含まれており、この要素には名前が"orderid"である属性が含まれています。"shiporder"要素には、"orderperson"、"shipto"、および"item"という3つの異なる子要素が含まれています。"item"要素は2回出現しており、"title"、任意の"note"要素、"quantity"、および"price"要素を含んでいます。
この行 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" は、XML解析器が特定の schema に基づいてこのドキュメントを検証するように指示しています。この行:xsi:noNamespaceSchemaLocation="shiporder.xsd" は、schema の位置を指定しています(ここでは、"shiporder.xml" と同じフォルダにあります)。
XML Schema を作成します
今、上記の XML 文書に schema を作成する必要があります。
新しいファイルを開き、その名前を "shiporder.xsd" にします。schema を作成するには、単に XML 文書の構造に従って、発見した各要素を定義するだけで十分です。まずは標準の XML 声明を開始します:
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> ... ... </xs:schema>
上記の schema では、標準の命名空間 (xs) を使用しており、この命名空間は Schema 言語定義(Schema language definition)に関連付けられています。その標準値は http://www.w3.org/2001/XMLSchema です。
次に、"shiporder" 元素を定義する必要があります。この要素は属性を持ち、他の要素を含むため、複合型と認定されます。"shiporder" 元素のサブ要素は xs:sequence 元素で囲まれ、サブ要素の順序を定義します:
<xs:element name="shiporder"> <xs:complexType> <xs:sequence> ... ... </xs:sequence> ... </xs:complexType> </xs:element>
次に、"orderperson" 元素をシンプル型として定義する必要があります(これは属性や他の要素を含まないためです)。タイプ (xs:string) のプレフィックスは、命名空間のプレフィックスによって規定され、この命名空間は、指示されたプレデファインドの schema データタイプの XML schema に関連付けられています:
<xs:element name="orderperson" type="xs:string"/>
次に、私は二つの要素を複合型として定義する必要があります:"shipto" と "item"。まず "shipto" 元素を定義します:
<xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
schema を通じて、maxOccurs と minOccurs 属性を使用して、ある要素がどれだけ出現するかを定義できます。maxOccurs はある要素が出現する回数の最大値を定義し、minOccurs は最小値を定義します。maxOccurs と minOccurs のデフォルト値はどちらも 1 です!
現在、"item" 元素を定義できます。この要素は "shiporder" 元素内に複数回出現することができます。これを実現するために、"item" 元素の maxOccurs 属性の値を "unbounded" に設定することで、"item" 元素は作成者が希望する任意の回数出現することができます。注意してください、"note" 元素はオプションの要素です。すでに、この要素の minOccurs 属性を 0 に設定しています:
<xs:element name="item" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minOccurs="0"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element>
今や「shiporder」要素の属性を宣言することができます。これは必須属性であるため、use="required"と定義しています。
注釈:この属性の宣言は最後に配置されなければなりません:
<xs:attribute name="orderid" type="xs:string" use="required"/>
これは「shiporder.xsd」という名前の schema ファイルのドキュメントリストです:
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="shiporder"> <xs:complexType> <xs:sequence> <xs:element name="orderperson" type="xs:string"/> <xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="item" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minOccurs="0"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="orderid" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:schema>
Schemaの分割
前の設計方法は非常に簡単ですが、文書が複雑になると読みやすく、保守が難しくなります。
次に紹介する設計方法は、まずすべての要素と属性の定義を行い、それから ref 属性を使用してそれらを参照する方法に基づいています。
これは新しい方法で設計された schema ファイルです:
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- 簡易要素の定義 --> <xs:element name="orderperson" type="xs:string"/> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> <!-- 属性の定義 --> <xs:attribute name="orderid" type="xs:string"/> <!-- 組合要素の定義 --> <xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="address"/> <xs:element ref="city"/> <xs:element ref="country"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="item"> <xs:complexType> <xs:sequence> <xs:element ref="title"/> <xs:element ref="note" minOccurs="0"/> <xs:element ref="quantity"/> <xs:element ref="price"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="shiporder"> <xs:complexType> <xs:sequence> <xs:element ref="orderperson"/> <xs:element ref="shipto"/> <xs:element ref="item" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute ref="orderid" use="required"/> </xs:complexType> </xs:element> </xs:schema>
指定された型(名前付き型)を使用します
第3種の設計方法は、クラスや型を定義し、要素の定義を再利用する能力を持たせます。具体的には、まずシンプル要素と複合要素に名前を付け、要素のtype属性を通じてそれらを指し示す方法です。
これは第3種の方法で設計されたschemaファイル("shiporder.xsd")です:
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="stringtype"> <xs:restriction base="xs:string"/> </xs:simpleType> <xs:simpleType name="inttype"> <xs:restriction base="xs:positiveInteger"/> </xs:simpleType> <xs:simpleType name="dectype"> <xs:restriction base="xs:decimal"/> </xs:simpleType> <xs:simpleType name="orderidtype"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{6}"/> </xs:restriction> </xs:simpleType> <xs:complexType name="shiptotype"> <xs:sequence> <xs:element name="name" type="stringtype"/> <xs:element name="address" type="stringtype"/> <xs:element name="city" type="stringtype"/> <xs:element name="country" type="stringtype"/> </xs:sequence> </xs:complexType> <xs:complexType name="itemtype"> <xs:sequence> <xs:element name="title" type="stringtype"/> <xs:element name="note" type="stringtype" minOccurs="0"/> <xs:element name="quantity" type="inttype"/> <xs:element name="price" type="dectype"/> </xs:sequence> </xs:complexType> <xs:complexType name="shipordertype"> <xs:sequence> <xs:element name="orderperson" type="stringtype"/> <xs:element name="shipto" type="shiptotype"/> <xs:element name="item" maxOccurs="unbounded" type="itemtype"/> </xs:sequence> <xs:attribute name="orderid" type="orderidtype" use="required"/> </xs:complexType> <xs:element name="shiporder" type="shipordertype"/> </xs:schema>
restriction 元素表明数据类型来源于 W3C XML Schema 命名空间的数据类型。因此,下面的片段也就意味着元素或属性的值必须是字符串类型的值:
<xs:restriction base="xs:string">
restriction 元素通常用于对元素施加限制。以下是一些来自上述 schema 的片段:
<xs:simpleType name="orderidtype"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{6}"/> </xs:restriction> </xs:simpleType>
このコードは、要素または属性の値が文字列でなければならず、連続する6文字でなければならず、これらの文字が0-9の数字であることを示しています。