XSD 要素置換(Element Substitution)
- 前のページ XSD <anyAttribute>
- 次のページ XSD例
XML Schemaを通じて、1つの要素が別の要素を置換することができます。
要素置換
例を示します:ユーザーは英国やノルウェーから来ています。ユーザーがXMLドキュメントでノルウェーの要素名を使用するか、英語の要素名を使用するかを選択できるようにする必要があります。
この問題を解決するために、XMLスキーマで以下を定義することができます: substitutionGroupまず、主要要素を宣言し、次にその次要素を宣言します。これらの次要素は主要要素を置き換えることができます。
<xs:element name="name" type="xs:string"/> <xs:element name="navn" substitutionGroup="name"/>
上記の例では、「name」要素が主要要素であり、「navn」要素が「name」要素を置き換えることができます。
以下のXMLスキーマの一部を見てください:
<xs:element name="name" type="xs:string"/> <xs:element name="navn" substitutionGroup="name"/> <xs:complexType name="custinfo"> <xs:sequence> <xs:element ref="name"/> </xs:sequence> </xs:complexType> <xs:element name="customer" type="custinfo"/> <xs:element name="kunde" substitutionGroup="customer"/>
合法なXMLドキュメントは以下の様になければなりません(上記のスキーマに基づいて):
<customer> <name>John Smith</name> </customer>
または以下の様に:
<kunde>John Smith
要素の置き換えを防ぐ
特定の要素が他の要素に置き換われないようにするには、block属性を使用してください:
<xs:element name="name" type="xs:string" block="substitution"/>
以下のXMLスキーマの一部を見てください:
<xs:element name="name" type="xs:string" block="substitution"/> <xs:element name="navn" substitutionGroup="name"/> <xs:complexType name="custinfo"> <xs:sequence> <xs:element ref="name"/> </xs:sequence> </xs:complexType> <xs:element name="customer" type="custinfo" block="substitution"/> <xs:element name="kunde" substitutionGroup="customer"/>
合法なXMLドキュメントは以下の様になければなりません(上記のスキーマに基づいて):
<customer> <name>John Smith</name> </customer>
しかし以下のドキュメントはもはや有効ではありません:
<kunde>John Smith
使用 substitutionGroup
可替换元素的类型必须和主元素相同,或者从主元素衍生而来。假如可替换元素的类型与主元素的类型相同,那么您就不必规定可替换元素的类型了。
请注意,substitutionGroup 中的所有元素(主元素和可替换元素)必须被声明为全局元素,否则就无法工作!
グローバル要素(Global Elements)とは何ですか?
グローバル要素は"schema"要素の直接子要素を指します!ローカル要素(Local elements)は他の要素に埋め込まれた要素を指します。
- 前のページ XSD <anyAttribute>
- 次のページ XSD例