ホーム HTML CSS XML JAVASCRIPT   PHP SQL MORE...   リファレンス 事例集    

XSD

« 前章へ 次章へ »

この章は、XMLスキーマの記述法のデモです。また、スキーマが異なる方法で記述できることも学習します。


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>John Smith</orderperson>
  <shipto>
    <name>Ola Nordmann</name>
    <address>Langgt 23</address>
    <city>4000 Stavanger</city>
    <country>Norway</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文書は、 "orderid" という必須の属性を含むルート要素、"shiporder" から構成されています。 "shiporder" 要素には、次の3つの子要素が含まれます : "orderperson"、"shipto"、"item"。 "item"要素は 2 回出現し、"title"、オプションの "note" 要素、 "quantity"、および "price" 要素が含まれます。

上の行: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" は、 この文書がスキーマに対して妥当でなければならないことをXMLパーサーに指示しています。 行: xsi:noNamespaceSchemaLocation="shiporder.xsd" は、スキーマが存在する場所を指定しています (ここでは "shiporder.xml" と同じフォルダにあります)。


XML スキーマの作成

さて、上記 XML 文書のスキーマを作成しようと思います。

"shiporder.xsd" と言う新しいファイルを開くことからスタートします。 スキーマを作成するには、単純にXML文書の構造に従って、見つけた各要素を定義します。 スキーマを定義する xs:schema 要素に続けて、標準の XML を宣言することで開始します:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
</xs:schema>

上記のスキーマでは標準的な名前空間(xs)を使用しています。 この名前空間に関連付けた URI は、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)には、定義済みのスキーマ・データ型を表すXMLスキーマに関連付けられた名前空間接頭辞が付けます:

<xs:element name="orderperson" type="xs:string"/>

次に、複合型の2つの要素: "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>

スキーマを使用して、maxOccurs属性やminOccurs属性の属性を持った要素の出現可能な回数を定義することができます。 maxOccurs 属性は、要素に対する出現数の最大値を指定し、minOccurs属性は、要素に対する出現数の最小値を指定します。 maxOccurs 属性と minOccurs 属性の両方のデフォルト値は 1 です!

今、"item" 要素​​を定義することができます。この要素は、"shiporder" 要素内に複数回出現することができます。 これは、作者が希望する "item" 要素の出現数分存在することができるように、"item" 要素​​の maxOccurs 属性を "unbounded" に設定することで指定します。 "note" 要素が省略可能であることに注意してください。minOccurs 属性をゼロに設定することで、これを指定します:

<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" というスキーマファイルの完全なリストは次のとおりです:

<?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>


スキーマの分割

前の設計法は、非常にシンプルですが、文書が複雑になると読取りや保守が困難になる可能性があります。

次の設計法は、最初にすべての要素と属性を定義することに基いて、ref 属性を使用してそれらを参照するようにします。

ここに、スキーマファイル("shiporder.xsd")の新しいデザインは、次のとおりです:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- definition of simple elements -->
<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"/>

<!-- definition of attributes -->
<xs:attribute name="orderid" type="xs:string"/>

<!-- definition of complex elements -->
<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>


名前付き型の使用

第三の設計手法は、要素の定義を再利用できるようにするクラスまたは型を定義します。 これは、simpleType と complexType 要素に名前を付けることで行われ、その後、要素の type 属性を通してポイントします。

ここで、スキーマファイル("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 要素は、要素に制約を適用するためにしばしば使用されます。 上記のスキーマから次の行を見てみましょう:

<xs:simpleType name="orderidtype">
  <xs:restriction base="xs:string">
    <xs:pattern value="[0-9]{6}"/>
  </xs:restriction>
</xs:simpleType>

これは、要素または属性の値は文字列で、連続した6文字丁度で、文字は 0 から 9 までの数字でなければならないこと示しています。


« 前章へ 次章へ »