XSD 元素替代

With XML Schemas, one element can substitute another element.
用XML Schema,一个元素可替代另一元素。


Element Substitution
元素替代(Element Substitution)

Let's say that we have users from two different countries: England and Norway. We would like the ability to let the user choose whether he or she would like to use the Norwegian element names or the English element names in the XML document.
假设有两个分别来自英国和挪威的使用者,我们很希望有能力能让他或她进行选择,在XML文档里的元素名称中选择他们所擅长的语言,是英文呢?还是挪威文呢?

To solve this problem, we could define a substitutionGroup in the XML schema. First, we declare a head element and then we declare the other elements which state that they are substitutable for the head element.
为解决这个问题,我们在XML schema里定义了替代组。首先,我们声明了一个标题元素,接着,我们声明已说明可替代标题元素的其他元素

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

In the example above, the "name" element is the head element and the "navn" element is substitutable for "name".
在上面的例子里,"name"元素是标题元素,"navn"元素可以替代"name"。

Look at this fragment of an XML schema:
看下面的XML schema片段:

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

A valid XML document (according to the schema above) could look like this:
一份有效 的XML文档(根据上述schema的XML文档)应该像这样:

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

</customer>

or like this:
或者像这样:

<kunde>

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


Blocking Element Substitution
关闭元素替代(Element Substitution)

To prevent other elements from substituting with a specified element, use the block attribute:
为了防止其他元素被已指定的元素替代(Element Substitution),可以用block属性:

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

Look at this fragment of an XML schema:
看这段XML schema片段:

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

A valid XML document (according to the schema above) looks like this:
一份有效的XML文档(根据上述的schema的XML文档)应该像这样:

<customer>

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

BUT THIS IS NO LONGER VALID:
但是这样就不再有效了

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


Using substitutionGroup
使用替代组(substitutionGroup)

The type of the substitutable elements must be the same as, or derived from, the type of the head element. If the type of the substitutable element is the same as the type of the head element you will not have to specify the type of the substitutable element.
可替代元素类型应和标题元素的类型相同,或是从中派生出来的。如果可替代元素类型和标题元素的类型相同,你就不需要再指明可替代元素的类型了

Note that all elements in the substitutionGroup (the head element and the substitutable elements) must be declared as global elements, otherwise it will not work!
注意在可替代元素组里的所有元素(标题元素和可替代元素)必须声明为“全域元素(global element)”,否则它是不会作用的!


What are Global Elements?
什么是“全域元素”?

Global elements are elements that are immediate children of the "schema" element! Local elements are elements nested within other elements.
“全域元素”是"schema"元素下面的直接子元素。“本地元素”是嵌套在别的元素里的元素。

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值