[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XML Schema views - are they possible?


xml schema element mandatory
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@w....

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.