XSD 元素替代

来源:互联网 发布:数控车整个圆球的编程 编辑:程序博客网 时间:2024/04/28 12:26

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"元素下面的直接子元素。“本地元素”是嵌套在别的元素里的元素。

 
原创粉丝点击