I'm trying to redefine the maxOccurs
attribute of an element in a simple XML Schema using Eclipse's WTP plugin as my IDE.
File: 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>
File: widget2.xsd
In this file I want to redefine the maxOccurs
attribute for Widget
to 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>
However, validation fails on widget2.xsd and Eclipse reports this error
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.
I tried replacing Widgets
in the <redefine>
with tns:Widgets
hoping to get rid of the namespace error but that doesn't work either.
What does this error mean? And is what I'm trying to do possible at all?
Ok, I managed to figure this one out after a lot of trial and error! The problem seems to have been that in widget1.xsd
the type for the Widgets
element was being created as an anonymous local type. Once I separated the type into its own local WidgetsType
it fixed the problem. I'd appreciate it if anybody could answer why. I'm pasting the modified files, maybe it'll help someone else.
File: 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>
File: 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>
The first include in your first post ist not allowed. Redefine acts like include too.
And you have to declare, the type in your redefined-Element. You can't just make it anonymous.
Your solution works because in the original widget2.xsd your try to redefine
the element Widgets
", but redefine only allows you to redefine types ... that's what you tried in your solution.
精彩评论