XSD 空要素


空の複合要素は内容を含むことができず、属性のみを含むことができます。


複合空要素

空のXML要素:

<product prodid="1345" />

上記の "product" 要素にはまったく内容がありません。内容のない型を定義するには、その内容に要素のみを含めることができる型を宣言する必要がありますが、実際には要素を一切宣言しません。例えば次のようにします:

<xs:element name="product">
  <xs:complexType>
    <xs:complexContent>
      <xs:restriction base="xs:integer">
        <xs:attribute name="prodid" type="xs:positiveInteger"/>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>
</xs:element>

上記の例では、複合コンテンツを持つ複合型を定義しました。complexContent 要素は、複合型のコンテンツモデルを制限または拡張することを示すシグナルであり、integer 制限は属性を宣言しますが、要素の内容は一切導入しません。

ただし、この "product" 要素をより簡潔に宣言することもできます:

<xs:element name="product">
  <xs:complexType>
    <xs:attribute name="prodid" type="xs:positiveInteger"/>
  </xs:complexType>
</xs:element>

または、complexType 要素に名前を付け、 "product" 要素の type 属性を設定してこの complexType 名を参照することもできます(この方法を使用すると、複数の要素が同じ複合型を参照できます):

<xs:element name="product" type="prodtype"/>

<xs:complexType name="prodtype">
  <xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>
その他の拡張