【发布时间】:2013-06-24 14:42:44
【问题描述】:
我认为我需要做的事情在 XSD 1.0 中是不可能的,但无论如何我会问...
我在一个文件中有一个complexType,比如a.xsd。原则上,我不能触摸这个文件。特别是,我无法更改其targetNamespace。一个例子是:
<xs:schema targetNamespace="http://myns.original"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:orig="http://myns.original">
<xs:element name="config" type="orig:ConfigType"/>
<xs:complexType name="ConfigType">
<xs:sequence>
<xs:element name="fieldA" type="xs:integer" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
我有第二个文件b.xsd,我在其中扩展在a.xsd 中定义的类型,并使用substitutionGroup 重新定义先前在a.xsd 中定义的元素。现在一切都很好,下面的例子似乎还可以:
<xs:schema targetNamespace="http://myns.myns"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:myns="http://myns.myns" xmlns:orig="http://myns.original">
<xs:import namespace="http://myns.original" schemaLocation="a.xsd"/>
<xs:complexType name="ConfigType">
<xs:complexContent>
<xs:extension base="orig:ConfigType">
<xs:sequence>
<xs:element name="fieldB" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="config" type="myns:ConfigType" substitutionGroup="orig:config"/>
</xs:schema>
问题来了:原始complexType 中的一个字段是可选 (minOccurs=0)。现在,我需要重新定义这种类型,以便该字段是强制性的 (minOccurs=1)。我猜想这可以通过xsd:redefine 来实现,所以我尝试了以下方法:
<xs:schema targetNamespace="http://myns.myns"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:myns="http://myns.myns">
<xs:redefine schemaLocation="b.xsd">
<xs:complexType name="ConfigType">
<xs:complexContent>
<xs:restriction base="myns:ConfigType">
<xs:sequence>
<xs:element name="fieldA" minOccurs="1"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:redefine>
</xs:schema>
但我收到以下消息:
There is not a complete functional mapping between the particles.
Error for type 'ConfigType'. The particle of the type is not a valid restriction of the particle of the base.
老实说,我不太了解这些消息,但经过一番调查,似乎实际问题是重新定义的字段必须与重新定义属于同一个命名空间。在我的例子中,我尝试将字段orig:fieldA 限制在命名空间http://myns.original 中,在一个targetNamespace="http://myns.myns" 的文件中。当然,如果像我在 b.xsd 中所做的那样继续扩展 c.xsd 中的类型,那没有问题,因为我不会尝试从不同的命名空间修改任何内容。
有人知道这是否可以实现吗?一种解决方案是使用正确的targetNamespace 复制将在不同文件a_2.xsd 中修改的定义。但对于复杂系统来说,这是一个非常不受欢迎且难以维护的解决方案。
【问题讨论】:
-
你可以用 XSD 1.1 做到这一点,我仍在寻找 1.0 中的解决方案