2013-10-03 59 views
1

我有一个具有本地complexType元素的模式,如下所示。XSD对本地complexType元素的限制

<xs:complexType name="AddressType"> 
    <xs:sequence> 
     <xs:element name="Line1" type="xs:string" minOccurs="0"/> 
     <xs:element name="Line2" type="xs:string" minOccurs="0" maxOccurs="100"/> 
     <xs:element name="Location" minOccurs="0" maxOccurs="100"> 
      <xs:complexType> 
       <xs:sequence> 
        <xs:element name="XCoordinate" type="xs:decimal" minOccurs="0"/> 
        <xs:element name="YCoordinate" type="xs:decimal" minOccurs="0"/> 
       </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
    </xs:sequence> 
</xs:complexType> 

我想如下

<xs:complexType name="InternalAddressType"> 
    <xs:complexContent> 
     <xs:restriction base="AddressType"> 
      <xs:sequence> 
       <xs:element name="Location" > 
       </xs:element> 
      </xs:sequence> 
     </xs:restriction> 
    </xs:complexContent> 
</xs:complexType> 

我收到以下错误

**Error for type 'InternalAddressType'. The particle of the type is not a valid restriction of the particle of the base. 

任何人都可以请帮助我了解什么我拨错做扩展这个复杂类型。看来问题在于该位置是本地ComplexType。但我无法改变,因为我从客户端获得了xsd,并且只需要扩展Loaction。我该如何解决这种情况。欢迎任何其他建议。

回答

0

您是对的:您不能限制Location,因为它由本地complexType定义。

即使AddressTyperestriction包括完全相同的部件同样会失败:没有尝试覆盖Location

<xs:complexType name="InternalAddressType"> 
    <xs:complexContent> 
     <xs:restriction base="AddressType"> 
      <xs:sequence> 
      <xs:element name="Line1" type="xs:string" minOccurs="0"/> 
      <xs:element name="Line2" type="xs:string" minOccurs="0" maxOccurs="100"/> 
      <xs:element name="Location" minOccurs="0" maxOccurs="100"> 
       <xs:complexType> 
       <xs:sequence> 
        <xs:element name="XCoordinate" type="xs:decimal" minOccurs="0"/> 
        <xs:element name="YCoordinate" type="xs:decimal" minOccurs="0"/> 
       </xs:sequence> 
       </xs:complexType> 
      </xs:element> 
      </xs:sequence> 
     </xs:restriction> 
    </xs:complexContent> 
    </xs:complexType> 

,上面的定义就可以了。

鉴于您无法更改AddressType以拉出当地complexType,您可以做什么?根据您的要求,也许您可​​以使用xs:extension扩展AddressType并根据需要定义您自己的MyLocation元素,而忽略原始Location元素(或者从不创建它 - 它是可选的)。或者,也许一个完全解耦的InternalAddressType定义适用于你。如果这些可能性都不能满足您的目的,请在评论中说明您的最终目标的要求,也许我们可以找到适合您的目标。

+0

嗨@kjhughes,谢谢你的回复。我想要做的是给出上述架构(“InternalAddressType”)我想创建另一个架构(“InternalAddressType_1”),它具有与上述架构相同的结构,但具有更多元素。我本来可以去扩展,但问题是它不会让我在“位置”上做任何更改。我将不得不创建一个名为Location_1的新类型并在其中添加元素。因此,我将不得不维护两个不同的位置结构,它们都有部分数据。如果有多个位置实例,哪个是问题。 – Rush

+0

计划进一步扩大/限制'InternalAddressType'到'InternalAddressType_1'只会加剧'AddressType'定义在'AddressType'(这是你无法控制的)方式的原始问题,它不能被覆盖。实际上,我认为你将不得不接受在给定这些约束的情况下你不能分享'AddressType'的'Location'的定义。 – kjhughes

0

如果您能够使用XSD 1.1(可悲的是,并不是很多人),那么您可以以断言的形式定义您的限制 - 这是因为它明确指出要应用的条件是什么其他条件是在我看来,一个更有用的机制,比提供一个必须保持与原始密切对应的替代内容模型。

XSD 1.1目前在Saxon和Xerces中实施。

另一种解决方案是两阶段验证。使用客户端模式来确保文档符合客户端定义的约束,然后使用您自己的模式(或其他技术)来验证它是否符合您定义的附加约束。无论如何,这可能是一个建筑清洁的方法。