代码之家  ›  专栏  ›  技术社区  ›  Praetorian

XML架构重新定义错误

  •  1
  • Praetorian  · 技术社区  · 14 年前

    我正在尝试重新定义 maxOccurs 使用Eclipse的wtp插件作为我的IDE的简单XML模式中元素的属性。

    文件:widget1.xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema targetNamespace="http://www.example.org/widget"
      elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:tns="http://www.example.org/widget">
    
      <xsd:complexType name="WidgetType">
        <xsd:sequence>
          <xsd:element name="Name" type="xsd:string"/>
          <xsd:element name="ProductID" type="xsd:unsignedInt"/>
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:element name="Widgets">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="Widget" type="tns:WidgetType" minOccurs="1" maxOccurs="65536"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    
    </xsd:schema>
    

    文件:widget2.xsd 在此文件中,我要重新定义 最大发生 属性为 Widget 到10。

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema targetNamespace="http://www.example.org/widget" elementFormDefault="qualified"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/widget">
    
      <xsd:include schemaLocation="widget1.xsd"/>
    
      <xsd:redefine schemaLocation="widget1.xsd">
        <xsd:complexType name="Widgets">
          <xsd:complexContent>
            <xsd:restriction base="Widgets">
              <xsd:sequence>
                <xsd:element name="tns:Widget" maxOccurs="10"/>
              </xsd:sequence>
            </xsd:restriction>
          </xsd:complexContent>
        </xsd:complexType>
      </xsd:redefine>
    
    </xsd:schema>
    

    但是,在widget2.xsd上验证失败,Eclipse报告此错误

    Multiple annotations found at this line:

        - src-resolve.4.1: Error resolving component 'Widgets'. It was detected that 'Widgets' has no namespace, but components with no target namespace are not referenceable from schema document 'file:///C:/Projects/Test/XMLSchema/Widget/widget2.xsd'. If 'Widgets' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'Widgets' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:///C:/Projects/Test/XMLSchema/Widget/widget2.xsd'.
    
        - src-redefine.5.b.d: 'restriction' does not have a 'base' attribute that refers to the redefined element, 'http://www.example.org/widget,Widgets'. <complexType> children of <redefine> elements must have <extension> or <restriction> descendants, with 'base' attributes that refer to themselves.
    

    我试着换了 Widgets <redefine> 具有 tns:Widgets 希望消除名称空间错误,但这也不起作用。

    这个错误是什么意思?我到底想做什么?

    3 回复  |  直到 8 年前
        1
  •  5
  •   Praetorian    14 年前

    好吧,经过多次尝试和错误,我终于找到了这个!问题似乎是 widget1.xsd 的类型 Widgets 元素被创建为匿名本地类型。一旦我把这个类型划分成它自己的本地类型 WidgetsType 它解决了这个问题。如果有人能回答原因,我将不胜感激。我正在粘贴修改过的文件,也许它可以帮助其他人。

    文件:widget1.xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema targetNamespace="http://www.example.org/widget"
      elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:tns="http://www.example.org/widget">
    
      <xsd:complexType name="WidgetType">
        <xsd:sequence>
          <xsd:element name="Name" type="xsd:string"/>
          <xsd:element name="ProductID" type="xsd:unsignedInt"/>
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:complexType name="WidgetsType">
        <xsd:sequence>
          <xsd:element name="Widget" type="tns:WidgetType" minOccurs="1" maxOccurs="65536"/>
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:element name="Widgets" type="tns:WidgetsType"/>
    
    </xsd:schema>
    

    文件:widget2.xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema targetNamespace="http://www.example.org/widget"
      elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:tns="http://www.example.org/widget">
    
      <xsd:redefine schemaLocation="widget1.xsd">
        <xsd:complexType name="WidgetsType">
          <xsd:complexContent>
            <xsd:restriction base="tns:WidgetsType">
              <xsd:sequence>
                <xsd:element name="Widget" type="tns:WidgetType" maxOccurs="10"/>
              </xsd:sequence>
            </xsd:restriction>
          </xsd:complexContent>
        </xsd:complexType>
      </xsd:redefine>
    
      <xsd:element name="Widgets" type="tns:WidgetsType" />
    
    </xsd:schema>
    
        2
  •  1
  •   Evgeny    12 年前

    第一个包含在您的第一个post ist中是不允许的。重新定义类似include的行为。

    必须声明,重新定义的元素中的类型。你不能让它匿名。

        3
  •  1
  •   Praetorian    8 年前

    您的解决方案之所以有效,是因为 WIDGET2.xSD 你尝试 redefine 元素 Widgets “,但重定义只允许您重定义类型…这就是你在解决方案中所尝试的。