2003年度 データベース論

XML Schema (3)
ソフトウェア特論
第5回 / 2004-05-28
お知らせ

6月11日 (金) の講義は休講となります。
きょうの目標


XML Schema の組み込みのデータ型につ
いて学ぶ。
XML Schema の複合型について学ぶ。
要素の出現回数の指定
要素の出現回数の指定

ある要素が




2回以上5回まで出現する
0回以上3回まで出現する
2回以上無制限に出現する
というような指定ができる
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>
minOccurs 属性と
maxOccurs 属性の指定
<xsd:element ref="st:email"
minOccurs="0" maxOccurs="2" />

e-mail 要素が0回以上、2回以下出現する。
minOccurs 属性


最低出現回数を示す
デフォルト値 (minOccurs 属性が指定され
ていない場合) は 1
maxOccurs 属性


最高出現回数を示す
回数が無制限の場合は、”unbounded” を
指定する。


maxOccurs = “unbounded”
デフォルト値 (maxOccurs 属性が指定さ
れていない場合) は 1
XML Schema に適合する
インスタンス (1)
<st:familyName>安藤</st:familyName>
<st:givenName>友晴</st:givenName>
<st:email>
[email protected]
</st:email>
XML Schema に適合する
インスタンス (2)
<st:familyName>安藤</st:familyName>
<st:givenName>友晴</st:givenName>

email 要素がなくてもよい。
XML Schema に適合しない例
<st:familyName>安藤</st:familyName>
<st:givenName>友晴</st:givenName>
<st:email>
[email protected]
</st:email>
<st:email>[email protected]</st:email>
<st:email>[email protected]</st:email>
sequence
<xsd:sequence>
要素が順番に出現する
<xsd:sequence>
<xsd:element
name="familyName“ type="xsd:string"/>
<xsd:element
name="givenName" type="xsd:string" />
</xsd:sequence>


familyName 要素が出てから givenName 要素が出現
順番の変更や要素の省略は許されない
XML Schema に適合する例
<st:student>
<st:familyName>安藤</st:familyName>
<st:givenName>友晴</st:givenName>
</st:student>
XML Schema に
適合しない例 (1)
<st:student>
<st:givenName>友晴</st:givenName>
<st:familyName>安藤</st:familyName>
</st:student>

givenName と familyName が逆
XML Schema に
適合しない例 (2)
<st:student>
<st:familyName>安藤</st:familyName>
</st:student>

givenName がない
all
<xsd:all>
要素が順不同に出現する
<xsd:all>
<xsd:element ref="st:familyName" />
<xsd:element ref="st:givenName" />
<xsd:element ref="st:email" />
</xsd:all>


どのような順番で要素が出現しても構わない。
それぞれの出現回数は 0 回か 1 回
XML Schema に適合する例
<st:student>
<st:givenName>友晴</st:givenName>
<st:email>
[email protected]
</st:email>
<st:familyName>安藤</st:familyName>
</st:student>
ダメな XML Schema
<xsd:all>
<xsd:element ref="st:familyName" />
<xsd:element ref="st:givenName" />
<xsd:element ref="st:email“
minOccurs=“0”
maxOccurs=“3” />
</xsd:all>

maxOccurs = “3” という指定はできない。
choice
<xsd:choice>
どれか1つの要素が出現する
<xsd:choice>
<xsd:element ref="it:book" />
<xsd:element ref="it:cd" />
<xsd:element ref="it:dvd" />
</xsd:choice>

子要素のうち、どれか1つが出現する
XML Schema に適合する例
<it:item>
<it:book>蹴りたい背中</it:book>
</it:item>
XML Schema に適合しない例
<it:item>
<it:book>蹴りたい背中</it:book>
<it:dvd>ラスト サムライ</it:dvd>
</it:item>

book 要素と dvd 要素が同時に出現して
いる
属性の指定
どのように属性を指定するか



complexType 要素の中で指定する。
complexType 要素の内容の最後で指定
する。
属性に指定には、attribute 要素を利用す
る。
XML Schema の例
<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>
XML Schema に適合する例
<st:student number="abcd“>
<st:familyName>
安藤
</st:familyName>
<st:givenName>友晴</st:givenName>
</st:student>
データの派生
データの派生とは


要素の内容にさらに細かい条件を付加し
た新しいデータ型を作成すること
データ型同士を組み合わせて新しいデー
タ型を作成すること
やりたいこと (1)


“userName” という要素を定義したい
“userName” に入るのは、最大でも8文字
の文字列
XML Schema の例
......
<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>
simpleType 要素

XML Schema 組み込みのデータ型をさら
に制限して使うような場合には、
simpleType 要素を用いる。
データの制限
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="8" />
</xsd:restriction>
</xsd:simpleType>

string 型の最大長を8文字に制限する
XML Schema に適合する例
<st:student>
<st:familyName>
安藤
</st:familyName>
<st:givenName>友晴</st:givenName>
<st:userName>tomoharu</st:userName>
</st:student>
やりたいこと (2)

student 要素の属性として「学籍番号」を
定義したい。

00-01-001 のように
XML Schema の例
......
<xsd:attribute ref="st:studentID" />
......
<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:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{2}-\d{2}-\d{3}" />
</xsd:restriction>
</xsd:simpleType>

pattern 要素の value 属性に正規表現を
指定できる。
“\d{2}-\d{2}-\d{3}”
という表現

“\d” は任意の数字を表す。



0 から 9 まで
{2} は2回の繰り返しを表す。
“数字が2回繰り返し” + “-” + “数字が2
回繰り返し” + “-” + “数字が3回繰り返し”

00-01-001 のような文字列にマッチ
XML Schema に適合する例
<st:student st:studentID="00-01-001“>
<st:familyName>安藤</st:familyName>
<st:givenName>友晴</st:givenName>
<st:email>
[email protected]
</st:email>
<st:userName>tomoharu</st:userName>
</st:student>
参考文献・URL

Extensible Markup Language (XML) 1.0
(Third Edition)



http://www.w3.org/TR/REC-xml/
XML 1.0 の仕様書です。
W3C XML Schema


http://www.w3.org/XML/Schema
XML Schema の仕様書など。