我正在尝试使用Eclipse的WTP插件作为我的IDE重新定义简单XML Schema中元素的maxOccurs
属性。
文件: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
在此文件中,我想将maxOccurs
的{{1}}属性重新定义为10。
Widget
但是,widget2.xsd上的验证失败,Eclipse报告此错误
<?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>
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>
,希望摆脱命名空间错误,但这也不起作用。
这个错误是什么意思?我正在尝试做什么呢?
答案 0 :(得分:5)
好的,经过大量的试验和错误,我成功地把这个想出来了!问题似乎是在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>
答案 1 :(得分:1)
首先包含在你的第一篇文章中不允许。重新定义的行为也包括在内。
你必须声明你重新定义的元素中的类型。你不能只是匿名。
答案 2 :(得分:1)
您的解决方案有效,因为在原始 widget2.xsd 中尝试redefine
元素Widgets
&#34;,但重新定义只允许您重新定义类型。那是你在解决方案中尝试过的。