XML Schema重定义错误

Pra*_*ian 1 xml xsd

我正在尝试maxOccurs使用Eclipse的WTP插件作为我的IDE,在简单的XML Schema中重新定义元素的属性.

文件: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>
Run Code Online (Sandbox Code Playgroud)

文件:widget2.xsd在这个文件中,我想将maxOccurs属性重新定义为Widget10.

<?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>
Run Code Online (Sandbox Code Playgroud)

但是,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.
Run Code Online (Sandbox Code Playgroud)

我试图取代Widgets<redefine>tns:Widgets希望摆脱命名空间的错误,但不工作的.

这个错误是什么意思?我正在尝试做什么呢?

Pra*_*ian 5

好吧,经过大量的试验和错误,我成功地把这个想出来了!问题似乎widget1.xsdWidgets元素的类型被创建为匿名本地类型.一旦我将类型分离到它自己的本地,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>
Run Code Online (Sandbox Code Playgroud)

文件: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>
Run Code Online (Sandbox Code Playgroud)