XSD インジケーター


インジケーターを使用することで、ドキュメント内での要素の使用方法を制御できます。


インジケーター

インジケーターは7種類あります:

Order インジケーター:

  • All
  • Choice
  • Sequence

Occurrence インジケーター:

  • maxOccurs
  • minOccurs

Group インジケーター:

  • Group name
  • attributeGroup name

Order インジケーター

Order インジケーターは要素の順序を定義するために使用されます。

All インジケーター

<all> インジケーターは、子要素が任意の順序で出現でき、各子要素が必ず1回だけ出現しなければならないことを規定します:

<xs:element name="person">
  <xs:complexType>
    <xs:all>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:all>
  </xs:complexType>
</xs:element>

注意:<all> インジケーターを使用する場合、<minOccurs> は 0 または 1 に設定でき、<maxOccurs> インジケーターは 1 にしか設定できません(<minOccurs> と <maxOccurs> については後で説明します)。

Choice インジケーター

<choice> インジケーターは、ある子要素または別の子要素のいずれか(二者択一)が出現できることを規定します:

<xs:element name="person">
  <xs:complexType>
    <xs:choice>
      <xs:element name="employee" type="employee"/>
      <xs:element name="member" type="member"/>
    </xs:choice>
  </xs:complexType>
</xs:element>

Sequence インジケーター

<sequence> は子要素が特定の順序で出現しなければならないことを規定します:

<xs:element name="person">
   <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>


Occurrence インジケーター

Occurrence インジケーターは、特定の要素が出現する頻度を定義するために使用されます。

注意:すべての "Order" および "Group" インジケーター(any、all、choice、sequence、group name および group reference)について、maxOccurs と minOccurs のデフォルト値は 1 です。

maxOccurs インジケーター

<maxOccurs> インジケーターは、要素が出現できる最大回数を規定できます:

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="full_name" type="xs:string"/>
      <xs:element name="child_name" type="xs:string" maxOccurs="10"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

上記の例は、子要素 "child_name" が "person" 要素内で最低1回(minOccurs のデフォルト値は 1)、最大10回出現できることを示しています。

minOccurs インジケーター

<minOccurs> インジケーターは、要素が出現できる最小回数を規定できます:

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="full_name" type="xs:string"/>
      <xs:element name="child_name" type="xs:string"
      maxOccurs="10" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

上記の例は、子要素 "child_name" が "person" 要素内で最低 0 回、最大 10 回出現できることを示しています。

ヒント:要素の出現回数を無制限にする場合は、maxOccurs="unbounded" という宣言を使用します:

実際の例:

「Myfamily.xml」という名前の XML ファイル:

<?xml version="1.0" encoding="ISO-8859-1"?>

<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="family.xsd">

<person>
  <full_name>Hege Refsnes</full_name>
  <child_name>Cecilie</child_name>
</person>

<person>
  <full_name>Tove Refsnes</full_name>
  <child_name>Hege</child_name>
  <child_name>Stale</child_name>
  <child_name>Jim</child_name>
  <child_name>Borge</child_name>
</person>

<person>
  <full_name>Stale Refsnes</full_name>
</person>

</persons>

上記の XML ファイルには、「persons」という名前のルート要素が含まれています。このルート要素内に、3つの「person」要素を定義しています。各「person」要素は「full_name」要素を1つ含む必要があり、同時に最大5つの「child_name」要素を含めることができます。

これはスキーマファイル「family.xsd」です:

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

<xs:element name="persons">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="person" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="full_name" type="xs:string"/>
            <xs:element name="child_name" type="xs:string"
            minOccurs="0" maxOccurs="5"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>


Group インジケーター

Group インジケーターは、関連する一連の要素群を定義するために使用されます。

要素グループ

要素グループは group 宣言によって定義されます:

<xs:group name="groupname">
...
</xs:group>

group 宣言内で all、choice、または sequence 要素を定義する必要があります。次の例は、「persongroup」という名前の group を定義しており、厳密な順序で出現しなければならない一連の要素を定義しています:

<xs:group name="persongroup">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="birthday" type="xs:date"/>
  </xs:sequence>
</xs:group>

group を定義した後、別の定義内でそれを参照できます:

<xs:group name="persongroup">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="birthday" type="xs:date"/>
  </xs:sequence>
</xs:group>

<xs:element name="person" type="personinfo"/>

<xs:complexType name="personinfo">
  <xs:sequence>
    <xs:group ref="persongroup"/>
    <xs:element name="country" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

属性グループ

属性グループは attributeGroup 宣言によって定義されます:

<xs:attributeGroup name="groupname">
...
</xs:attributeGroup>

次の例は、「personattrgroup」という名前の属性グループを定義しています:

<xs:attributeGroup name="personattrgroup">
  <xs:attribute name="firstname" type="xs:string"/>
  <xs:attribute name="lastname" type="xs:string"/>
  <xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>

属性グループを定義した後、別の定義内で次のように参照できます:

<xs:attributeGroup name="personattrgroup">
  <xs:attribute name="firstname" type="xs:string"/>
  <xs:attribute name="lastname" type="xs:string"/>
  <xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>

<xs:element name="person">
  <xs:complexType>
    <xs:attributeGroup ref="personattrgroup"/>
  </xs:complexType>
</xs:element>
その他の拡張