当前位置: 首页 > 网络学院 > XML相关教程 > Schema (XSD) > XSD 元素替代

Schema (XSD)
XSD 逻辑值数据类型
XML Schema总结
XML Schema 参考
XSD 验证

Schema (XSD) 中的 XSD 元素替代


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 773 ::
收藏到网摘: n/a

With XML Schemas, one element can substitute another element.
我们可以通过XML Schema实现一个元素替代另一元素。


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:
根据上述schema的XML文档,我们得到了 一份有效的XML文档,如下所示:

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

</customer>

or like this:
或者如下所示:

<kunde>

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

 


Blocking Element Substitution
锁定元素替代

To prevent other elements from substituting with a specified element, use the block attribute:
为了防止其它元素被已指定的元素替代,可以用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:
根据上述schema的XML文档,我们得到了 一份有效的XML文档,如下所示:

<customer>

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

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

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

 


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

评论 (0) All

登陆 | 还没注册?