简体   繁体   English

XSD对本地complexType元素的限制

[英]XSD Restriction on local complexType elements

I have a schema that has a local complexType element as follows. 我有一个具有本地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>

I am trying to extend this complexType as follows 我正在尝试如下扩展这个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>

I am getting the following error 我收到以下错误

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

Can anyone please help me understand what am I doing worng. 谁能帮我了解我在做什么。 It seems that the problem is that the Location is a local ComplexType. 看来问题在于位置是本地的ComplexType。 But I can not change that since I am getting the xsd from the client and need to extend just the Loaction. 但是我无法更改它,因为我是从客户端获取xsd的,只需要扩展Loaction。 How can I solve this situation. 我该如何解决这种情况。 Any other suggestions are welcome. 欢迎其他任何建议。

You're right: You can't restrict Location because it is defined by a local complexType . 您说对了:您不能限制Location因为它是由本地complexType定义的。

Even a restriction of AddressType that includes the exact same components will fail similarly: 甚至包括完全相同组件的AddressType restriction也将类似地失败:

  <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>

Without the attempt to override Location , the above definition would be fine. 如果不尝试覆盖Location ,那么上面的定义就可以了。

Given that you cannot change AddressType to pull out the local complexType , what can you do? 鉴于您无法更改AddressType来提取本地complexType ,该怎么办? Depending upon your requirements, perhaps you might extend AddressType using xs:extension and define your own MyLocation element that is as you wish, ignoring the original Location element (or never creating it -- it's optional). 根据您的要求,也许您可​​以使用xs:extension扩展AddressType并根据需要定义自己的MyLocation元素,而忽略原始的Location元素(或从不创建它-是可选的)。 Or, perhaps a fully decoupled definition of InternalAddressType works for you. 或者,也许完全解耦的InternalAddressType定义对您有用。 If neither of these possibilities satisfy your purposes, state in the comments the requirements of your final goal, and perhaps we can find something suitably close. 如果这些可能性都不满足您的目的,请在注释中说明您最终目标的要求,也许我们会找到合适的答案。

If you are able to use XSD 1.1 (sadly, not many people are), then you can define your restriction in the form of an assertion - which because it states precisely what additional conditions you want to apply, is a much more usable mechanism, in my opinion, than providing an alternative content model that has to maintain a close correspondence to the original. 如果您能够使用XSD 1.1(遗憾的是,没有多少人),则可以以断言的形式定义您的限制-因为它精确地指出了您想要应用的其他条件,因此它是一种更加实用的机制,我认为,与其提供必须与原始内容保持紧密对应关系的替代内容模型。

XSD 1.1 is currently implemented in Saxon and Xerces. XSD 1.1当前在Saxon和Xerces中实现。

The other solution is two-phase validation. 另一种解决方案是两阶段验证。 Use the client's schema to ensure that the document conforms to the constraints defined by the client, then use your own schema (or other technology) to validate that it conforms to the additional constraints defined by you. 使用客户端的架构来确保文档符合客户端定义的约束,然后使用您自己的架构(或其他技术)来验证文档是否符合您定义的其他约束。 That may well be an architecturally cleaner approach anyway. 无论如何,这可能是一种架构上更清洁的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM