簡単な方法の 1 つは、エレメント Layout を Type (必須)、Orientation、North、および Center (すべてオプション) という名前の属性を持つものとして宣言し、@Type = "Stack" の場合、Orientation は意味があり、North と Center は意味がないことを指定することです。 @Type = 'Margin' の場合、North と Center はこれまたはあれを意味し、Orientation は意味を持ちません。
これには、事実上すべてのスキーマ言語で実行できるという利点があり、物事を比較的単純に保つことができます。共起制約を検証する責任がバリデータから消費ソフトウェアに移るという欠点があります。
XSD 1.1 を使用する 2 番目の方法は、条件付きの型割り当てを使用することです。適切な属性を持つ 2 つの型を宣言し、それぞれが使用される条件を指定します。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.com/nss/layout"
targetNamespace="http://www.example.com/nss/layout"
elementFormDefault="qualified">
<!--* Strictly speaking, this type is not essential,
* but it gives an overview of the kinds of layout
* we expect and allows a reviewer to have more
* confidence that the conditional type assignment
* covers all the necessary bases. *-->
<xs:simpleType name="layout-types">
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="Stack" />
<xs:enumeration value="Margin" />
</xs:restriction>
</xs:simpleType>
<!--* Two different types for the two different
* kinds of layout. *-->
<xs:complexType name="StackLayout">
<xs:attribute name="Type"
type="tns:layout-types"
use="required"/>
<xs:attribute name="Orientation"
type="xs:string"
use="required"/>
</xs:complexType>
<xs:complexType name="MarginLayout">
<xs:attribute name="Type"
type="tns:layout-types"
use="required"/>
<xs:attribute name="North"
type="xs:int"
use="required"/>
<xs:attribute name="Center"
type="xs:int"
use="required"/>
</xs:complexType>
<!--* The Layout element is bound to type tns:StackLayout
* if its Type attribute has the value 'Layout'.
* Otherwise, it's bound to tns:MarginLayout, if its
* Type attribute = 'Margin'. Otherwise, the Layout
* element instance is invalid.
* If there's a default type you can use, you don't
* have to use xs:error.
*-->
<xs:element name="Layout">
<xs:alternative test="@Type='Layout'"
type="tns:StackLayout"/>
<xs:alternative test="@Type='Margin'"
type="tns:MarginLayout"/>
<xs:alternative type="xs:error"/>
</xs:element>
</xs:schema>
XSD 1.1 バリデーターにアクセスできない場合は、Schematron を使用して共起制約を確認するか、制約を検証する Relax NG スキーマを作成できます。