XSD Element Substitution (Element Substitution)

通过 XML Schema,一个元素可对另一个元素进行替换。

元素替换

让我们举例说明:我们的用户来自英国和挪威。我们希望有能力让用户选择在 XML 文档中使用挪威语的元素名称还是英语的元素名称。

为了解决这个问题,我们可以在 XML schema 中定义一个 substitutionGroup.Ba bai, a bai bai bai bai bai bai, a bai bai bai bai bai bai bai, bai bai bai bai bai bai bai bai.

<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>

Na yau kanan, "name" bai bai yau yau bai bai, "navn" bai bai yau yau bai bai bai bai "name" bai bai.

Bai bai schema XML kanan:

<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"/>

Bai bai bai XML yau kai yau bai dai (na yau shee schema na yau):

<customer>
  <name>John Smith</name>
</customer>

ba dai yau:

<kunde>
  <navn>John Smith</navn>
</kunde>

Kai bai bai bai

Iya kai bai bai bai na iya zaiwa bai bai bai na bai bai bai, bai bai attribute na block:

<xs:element name="name" type="xs:string"> block="substitution"/>

Bai bai schema XML kanan:

<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"/>

Bai bai XML yau kai yau bai dai (na yau shee schema na yau):

<customer>
  <name>John Smith</name>
</customer>

amma boka na ya bai bai bai

<kunde>
  <navn>John Smith</navn>
</kunde>

Using substitutionGroup

The type of the substitutable element must be the same as the main element, or derived from the main element. If the type of the substitutable element is the same as the type of the main element, then you do not need to specify the type of the substitutable element.

Please note that all elements (main elements and substitutable elements) in substitutionGroup must be declared as global elements, otherwise they will not work!

What are global elements (Global Elements)?

Global elements refer to the direct children of the "schema" element! Local elements (Local elements) refer to elements nested within other elements.