[
Lists Home |
Date Index |
Thread Index
]
Hi Andy,
First off, I'd recommend that you use an XML Schema validator other
than XML Spy's. Xerces or MSXML are likely to give you more conformant
and consistent results.
> What I would like to achieve is:
> 1. if I wanted to change the restriction values in the complexType,
> the change would ripple through to all elements using this
> complexType. However, this is not the case. When placing a
> 'restriction' element, in XMLSpy it duplicates all the elements from
> the complexType
>
> 2. If I didn't want this element to be passed at all ie make
> maxOccurs ='0' (PS is this bad coding?), I don't want the XML Schema
> to validate the element. After a few examples by putting the element
> in, the XML schema still does validate the element, although its not
> wanted.
What I'd do, firstly, is make sure that I was using named types in my
schema. When you're restricting a type, which entails repeating
element declarations from the base type, you need the element
declarations to have the same type as they have in the base type, and
the only way to achieve that is to use named types. So I'd change my
initial schema to contain:
<xs:complexType name="StatusType">
<xs:all>
<xs:element name="StatusCode" type="StatusCodeType"
minOccurs="0" />
<xs:element name="StatusMessage" type="StatusMessageType"
minOccurs="0" />
</xs:all>
</xs:complexType>
<xs:complexType name="StatusCodeType">
<xs:simpleContent>
<xs:restriction base="tns:stringItemType">
<xs:minLength value="1" />
<xs:maxLength value="10" />
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="StatusMessageType">
<xs:simpleContent>
<xs:restriction base="tns:stringItemType">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
This is your base schema. You can now define additional, revised
schemas on top of it, using <xs:redefine> to redefine the StatusType
as desired.
For example, to make the <StatusCode> element mandatory, use:
<xs:redefine schemaLocation="base.xsd">
<xs:complexType name="StatusType">
<xs:complexContent>
<xs:restriction base="StatusType">
<xs:all>
<xs:element name="StatusCode" type="StatusCodeType" />
<xs:element name="StatusMessage" type="StatusMessageType"
minOccurs="0" />
</xs:all>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:redefine>
To make <StatusCode> forbidden, don't include it in the redefined type
(you can't do maxOccurs="0" -- that's not legal XML Schema syntax):
<xs:redefine schemaLocation="base.xsd">
<xs:complexType name="StatusType">
<xs:complexContent>
<xs:restriction base="StatusType">
<xs:all>
<xs:element name="StatusMessage" type="StatusMessageType"
minOccurs="0" />
</xs:all>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:redefine>
Both of the changes that you want to make (making an optional element
mandatory, or excluding an optional element) are perfectly legal
restrictions.
The changes to the StatusType within the <xs:redefine> will ripple
through to all the elements of that type.
By the way, it's best to ask schema questions on xmlschema-dev@w3.org.
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
|