【问题标题】:Restrict element of type in other namespace限制其他命名空间中的类型元素
【发布时间】: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 中的解决方案

标签: xml xsd redefine xsd-1.0


【解决方案1】:

到目前为止,我看到的唯一问题是在架构 a.xsd 中您已定义:

<xs:element name="fieldA" type="xs:integer" minOccurs="0"/>

而在最后一个模式(重新定义)中,您有:

<xs:element name="fieldA" minOccurs="1"/>

最后一个声明的实际含义是您为fieldA 隐式指定xs:anyType

<xs:element name="fieldA" type="xs:anyType" minOccurs="1"/>

请记住,当您通过限制派生新类型时(实际上就是您 在重新定义中做),您必须重新定义元素内容模型。 但新的内容模型必须完全符合旧的内容。

在您的上一个架构中不是这种情况,因为之前 根据a.xsd,元素fieldA 被允许只有整数值。 但是现在,你说它可以接受任何东西。 这肯定会导致错误,并且您收到的消息(尽管确实是胡言乱语):

类型的粒子不是基粒子的有效限制。

似乎在说这个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-18
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多