Powered by SmartDoc

XML Schema (3)

複合型 (2)

要素の出現回数の指定

XML Schema の例

XML Schemaでは、ある要素が何回出現きるか指定できるようになっています。例えば、次のような指定になります。

出現回数を指定するためのXML Schemaを見てみましょう。

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wakhok.ac.jp/~tomoharu/students"
            xmlns:st="http://www.wakhok.ac.jp/~tomoharu/students">
    <xsd:element name="student">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="st:familyName" />
                <xsd:element ref="st:givenName" />
                <xsd:element ref="st:email" minOccurs="0" maxOccurs="2" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="familyName" type="xsd:string" />
    <xsd:element name="givenName" type="xsd:string" />
    <xsd:element name="email" type="xsd:string" />
</xsd:schema>

このXML Schemaでは、次の部分で出現回数を指定しています。ここでは、emailという要素が0回以上2回以下出現することを示しています。

     <xsd:element ref="st:email" minOccurs="0" maxOccurs="2" />

minOccurs 属性

minOccurs属性は、最低出現回数を示します。この例の場合は、0が指定されています。0ということは、最低でも0回、つまり出現しなくてもよいということになります。minOccurs属性が指定されていない場合のデフォルト値は1となっています。

maxOccurs 属性

maxOccurs属性は、最高出現回数を示します。この例の場合は、2が指定されています。最高でも2つまで、という意味です。回数が無制限の場合は、"unbounded"を指定します。

     <xsd:element ref="st:email" maxOccurs="unbounded" />

maxOccurs属性が指定されていない場合のデフォルト値は1となっています。

XML インスタンス

次の例は、このXML Schemaに適合するXMLインスタンス(の一部)です。

    <st:familyName>安藤</st:familyName>
    <st:givenName>友晴</st:givenName>
    <st:email>tomoharu@wakhok.ac.jp</st:email>

email要素が1回だけ出現しています。また、次のように、email要素が一度も出現していなくても適合します。

    <st:familyName>安藤</st:familyName>
    <st:givenName>友晴</st:givenName>

次の例では、email要素が3つ出てきているので、適合しません。

<st:familyName>安藤</st:familyName>
<st:givenName>友晴</st:givenName>
<st:email>tomoharu@wakhok.ac.jp</st:email>
<st:email>aaa@b.com</st:email>
<st:email>bbb@hogehoge.jp</st:email>

all 要素

前回、complexType要素の子要素として、sequence要素を紹介しました。complexType要素には、sequenceの他にも指定できる要素があります。

all要素は、子要素が順不同に出現することを表します。

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wakhok.ac.jp/~tomoharu/students"
            xmlns:st="http://www.wakhok.ac.jp/~tomoharu/students">
    <xsd:element name="student">
        <xsd:complexType>
            <xsd:all>
                <xsd:element ref="st:familyName" />
                <xsd:element ref="st:givenName" />
                <xsd:element ref="st:email" />
            </xsd:all>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="familyName" type="xsd:string" />
    <xsd:element name="givenName" type="xsd:string" />
    <xsd:element name="email" type="xsd:string" />
</xsd:schema>

all要素の部分だけを取り出してみましょう。

            <xsd:all>
                <xsd:element ref="st:familyName" />
                <xsd:element ref="st:givenName" />
                <xsd:element ref="st:email" />
            </xsd:all>

all要素は、子要素がどのような順番でも出現できます。sequence要素では順番通りにしか出現できなかったので、ここが大きく違うところです。

この例では、familyName, givenName, emailの各要素はどのような順番で出現しても構いません。

そして、all要素の子要素の出現回数は1回か0回となっています。2回以上は出現できません。

次の例は、このXML Schemaに適合するXMLインスタンス(の一部)です。

<st:student>
    <st:givenName>友晴</st:givenName>
    <st:email>
        tomoharu@wakhok.ac.jp
    </st:email>
    <st:familyName>安藤</st:familyName>
</st:student>

次のXML Schemaは、all要素中のemail要素にmaxOccurs属性があります。この属性の値が3になっているため、エラーになります。

<xsd:all>
    <xsd:element ref="st:familyName" />
    <xsd:element ref="st:givenName" />
    <xsd:element ref="st:email“
            minOccurs=“0”
            maxOccurs=“3” />
</xsd:all>

choice 要素

choice要素は、子要素のうちどれか1つの要素だけが出現することを表します。

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wakhok.ac.jp/~tomoharu/students"
            xmlns:it="http://www.wakhok.ac.jp/~tomoharu/students">
    <xsd:element name="item">
        <xsd:complexType>
            <xsd:choice>
                <xsd:element ref="it:book" />
                <xsd:element ref="it:cd" />
                <xsd:element ref="it:dvd" />
            </xsd:choice>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="book" type="xsd:string" />
    <xsd:element name="cd" type="xsd:string" />
    <xsd:element name="dvd" type="xsd:string" />
</xsd:schema>

choice要素の部分だけを取り出してみます。

            <xsd:choice>
                <xsd:element ref="it:book" />
                <xsd:element ref="it:cd" />
                <xsd:element ref="it:dvd" />
            </xsd:choice>

choice要素の子要素であるbook, cd, dvdのうち、一つの要素だけが出現することになります。

次の例は、このXML Schemaに適合するXMLインスタンス(の一部)です。item要素の子要素として、book要素が出現しています。

<?xml version="1.0"?>
<it:item xmlns:it="http://www.wakhok.ac.jp/~tomoharu/students"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.wakhok.ac.jp/~tomoharu/students \
    sample07.xsd">
    <it:book>蹴りたい背中</it:book>
</it:item>

次の例は、item要素の子要素としてbook要素とdvd要素が出現しているので、XML Schema文書に適合しません。

<?xml version="1.0"?>
<it:item xmlns:it="http://www.wakhok.ac.jp/~tomoharu/students"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.wakhok.ac.jp/~tomoharu/students \
    sample07.xsd">
    <it:book>蹴りたい背中</it:book>
    <it:dvd>ラスト サムライ</it:dvd>
</it:item>

属性の指定

XML Schemaで属性を指定するためには、attribute要素を利用します。

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wakhok.ac.jp/~tomoharu/students"
            xmlns:st="http://www.wakhok.ac.jp/~tomoharu/students">
    <xsd:element name="student">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="st:familyName" />
                <xsd:element ref="st:givenName" />
            </xsd:sequence>
            <xsd:attribute name="number" type="xsd:string" />
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="familyName" type="xsd:string" />
    <xsd:element name="givenName" type="xsd:string" />
</xsd:schema>

属性の指定に代わる部分だけを抜き出してみましょう。

        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="st:familyName" />
                <xsd:element ref="st:givenName" />
            </xsd:sequence>
            <xsd:attribute name="number" type="xsd:string" />
        </xsd:complexType>

attribute要素はcomplexType要素の中で指定します。その際には、complexType要素の内容の最後で指定する必要があります。

このXML Schemaに適合するXMLインスタンスは次のようになります。

<?xml version="1.0"?>
<st:student number="abcd"
          xmlns:st="http://www.wakhok.ac.jp/~tomoharu/students"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.wakhok.ac.jp/~tomoharu/students \
    sample08.xsd">
    <st:familyName>安藤</st:familyName>
    <st:givenName>友晴</st:givenName>
</st:student>

データの派生

派生

これまで見てきたように、XML Schemaには単純型と複合型という2種類のデータ型があります。こうしたデータ型を元にして、XML Schemaでは、要素の内容にさらに細かい条件を付加した新しいデータ型を作成できます。また、データ型同士を組み合わせて新しいデータ型を作成することもできます。こうしたことを「派生」と呼びます。

ここでは派生のうち、単純型にさらに細かい条件を付加する「制限」について取り上げます。

simpleType要素によるデータ型の制限

"userName"という名前の要素を定義し、そのuserNameには8文字までの文字列が入るようにしましょう。

XML Schema全体は次のようになります。

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wakhok.ac.jp/~tomoharu/students"
            xmlns:st="http://www.wakhok.ac.jp/~tomoharu/students">
    <xsd:element name="student">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="st:familyName" />
                <xsd:element ref="st:givenName" />
                <xsd:element ref="st:email" />
                <xsd:element ref="st:userName" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="familyName" type="xsd:string" />
    <xsd:element name="givenName" type="xsd:string" />
    <xsd:element name="email" type="xsd:string" />

    <xsd:element name="userName">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:maxLength value="8" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:element>

</xsd:schema>

userNameに関係する部分を抜き出すと、次のようになります。

        ........
        <xsd:element ref="st:userName" />
        ........

    <xsd:element name="userName">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:maxLength value="8" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:element>

XML Schema組み込みのデータ型をさらに制限して使うような場合には、simpleType要素を用います。

ここでは、simpleType要素の子要素であるrestriction要素、さらにその子要素であるmaxLength要素によって、userName要素の内容の文字列が最大8文字までと定義されています。

このXML Schemaに適合するXMLインスタンスを示しましょう。

<?xml version="1.0"?>
<st:student xmlns:st="http://www.wakhok.ac.jp/~tomoharu/students"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.wakhok.ac.jp/~tomoharu/students \
    sample09.xsd">
    <st:familyName>安藤</st:familyName>
    <st:givenName>友晴</st:givenName>
    <st:email>tomoharu@wakhok.ac.jp</st:email>
    <st:userName>tomoharu</st:userName>
</st:student>

正規表現の利用

次に、student要素の属性として、"00-01-001"のような学籍番号を定義することにしましょう。

まずは、XML Schemaからです。

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wakhok.ac.jp/~tomoharu/students"
            xmlns:st="http://www.wakhok.ac.jp/~tomoharu/students">
    <xsd:element name="student">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="st:familyName" />
                <xsd:element ref="st:givenName" />
                <xsd:element ref="st:email" />
                <xsd:element ref="st:userName" />
            </xsd:sequence>
            <xsd:attribute ref="st:studentID" />
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="familyName" type="xsd:string" />
    <xsd:element name="givenName" type="xsd:string" />
    <xsd:element name="email" type="xsd:string" />

    <xsd:element name="userName">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:maxLength value="8" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:element>

    <xsd:attribute name="studentID">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:pattern value="\d{2}-\d{2}-\d{3}" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:attribute>

</xsd:schema>

ここで特徴的なのは、データの制限に正規表現を利用していることです。pattern要素のvalue属性に正規表現を指定できます。

                <xsd:pattern value="\d{2}-\d{2}-\d{3}" />

この"\d{2}-\d{2}-\d{3}"という正規表現について詳しく見てみましょう。

まず、"\d"は0から9までの任意の数字を表します。そして、"{2}"は2回の繰り返しを表します。"-"はただの文字です。

まとめると、「数字が2回繰り返し」+「-」+「数字が2回繰り返し」+「-」+「数字が3回繰り返し」というパターンの文字列となります。これによって、"00-01-001"のような文字列にマッチできるのです。

最後に、XMLインスタンスを示しましょう。

<?xml version="1.0"?>
<st:student st:studentID="00-01-001"
          xmlns:st="http://www.wakhok.ac.jp/~tomoharu/students"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.wakhok.ac.jp/~tomoharu/students \
    sample11.xsd">
    <st:familyName>安藤</st:familyName>
    <st:givenName>友晴</st:givenName>
    <st:email>tomoharu@wakhok.ac.jp</st:email>
    <st:userName>tomoharu</st:userName>
</st:student>

参考文献・URL

Extensible Markup Language (XML) 1.0 (Third Edition)
W3C XML Schema