Xml 对本地complexType元素的XSD限制

Xml 对本地complexType元素的XSD限制,xml,xsd,restriction,complextype,Xml,Xsd,Restriction,Complextype,我有一个具有本地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

我有一个具有本地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>

谁能帮我理解我在做什么。问题似乎在于位置是本地ComplexType。但是我不能改变这一点,因为我从客户机获取xsd,并且只需要扩展Loaction。我怎样才能解决这个问题。欢迎任何其他建议。

你是对的:你不能限制位置,因为它是由本地complexType定义的

即使是包含完全相同组件的AddressType限制也会同样失败:

  <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的完全解耦定义适合您。如果这两种可能性都不能满足您的目的,请在注释中说明您最终目标的要求,也许我们可以找到一些合适的接近点。

如果您能够使用XSD 1.1,很遗憾,没有多少人能够,然后,您可以以断言的形式定义您的限制,因为它精确地说明了您想要应用的附加条件,在我看来,这是一种比提供一个必须与原始内容保持密切对应的替代内容模型更有用的机制

XSD1.1目前在Saxon和Xerces中实现


另一种解决方案是两阶段验证。使用客户机的模式确保文档符合客户机定义的约束,然后使用您自己的模式或其他技术验证文档是否符合您定义的其他约束。无论如何,这可能是一种架构上更干净的方法。

Hi@kjhughes,谢谢您的回复。我想做的是,给定上面的模式InternalAddressType,我想创建另一个模式InternalAddressType_1,它与上面的模式具有相同的结构,但包含更多的元素。我本来可以去延长,但问题是它不允许我在位置上做任何改变。我必须创建一个名为Location_1的新类型,并在其中添加元素。因此,我必须维护两个不同的位置结构,它们都有部分数据。如果存在多个位置实例,这是一个问题。计划进一步将InternalAddressType扩展/限制为InternalAddressType_1只会加剧最初的问题,即在AddressType中定义位置的方式不受您的控制,无法覆盖。实际上,我认为您必须接受这样一个事实:在这些约束条件下,您不能共享AddressType位置的定义。
**Error for type 'InternalAddressType'.  The particle of the type is not a valid restriction of the particle of the base.
  <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>