在 XML Schemas 中为 complexTypes 添加限制时,是否需要重写 complexType 定义中使用的所有元素?如果是这样,为什么它不能重用现有的元素定义并覆盖新的受限元素?
例如,在下面;当我只想限制字段 country 时,我应该重新重写所有 3 个字段吗?
<xs:complexType name="customer">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Norwegian_customer">
<xs:complexContent>
<xs:restriction base="customer">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="country" type="xs:string" fixed="Norway"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
所以,从下面的答案中可以清楚地看出为什么我们必须重写整个类型。
跟进问题
那么这个限制功能有什么用呢?
一种情况,我能想到;当您必须验证包含受限类型而不是 xml 模式中的基本类型的实例文档时。
说,如果“B”是基本类型并且它被限制为“B*”。在模式文档预期类型为“B”的元素的位置包含“B*”的任何实例文档都可以工作。我们不必为每个受限类型编写单独的规则。(属性“xsi:type”在实例文档将使用正确的类型对其进行验证。)对吗?
此功能还有其他用途吗?