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

An element with more than one possible type

From: pau carre <pau.carre@gmail.com>
Date: Fri, 2 Feb 2007 12:47:02 +0100
Message-ID: <4b7300ee0702020347g543fb44aic579ddd3aeaef0bb@mail.gmail.com>
To: xmlschema-dev@w3.org
An element with more than one possible type

I am trying to code a XML Schema (XSD file) for a web service in such a way:

<items>
 <item name = "pen" color ="22">
 <item name = "pencil" smoothness = "10" >
</items>

It is to say, if name is "pen" , then use the "color" attribute.
When name is "pencil" then use "smoothness" attribute.

What I have done is to define a simple type for name attribute

<xs:simpleType name="nameType">
      <xs:restriction base="xs:string">
              <xs:enumeration value="pen"/>
              <xs:enumeration value="pencil"/>
      </xs:restriction>
</xs:simpleType>

Then I defined an abstract complex type for "item" element:

<xs:complexType name="itemType" abstract = "true">
      <xs:attribute name = "name" type = "nameType"/>
</xs:complexType>

I restricted the "name" attribute to "pencil":

<xs:complexType name="itemTypePencil">
      <xs:complexContent>
              <xs:restriction base="itemType">
                      <xs:attribute name = "name" type = "nameType"
fixed="pencil"/>
              </xs:restriction>
      </xs:complexContent>
</xs:complexType>

.... and finally I added an attribute "smoothness":

<xs:complexType name="itemTypePencilSmoothness">
      <xs:complexContent>
              <xs:extension base="itemTypePencil">
                      <xs:attribute name = "smoothness" type = "xs:int"/>
              </xs:extension>
      </xs:complexContent>
</xs:complexType>

For the "pen" is the same as "pencil". First I restricted the "name"
attribute to "pen":

<xs:complexType name="itemTypePen">
      <xs:complexContent>
              <xs:restriction base="itemType">
                      <xs:attribute name = "name" type = "nameType"
fixed="pen"/>
              </xs:restriction>
      </xs:complexContent>
</xs:complexType>

.... and finally I added the "color" attribute

<xs:complexType name="itemTypePenColor">
      <xs:complexContent>
              <xs:extension base="itemTypePen">
                      <xs:attribute name = "color" type = "xs:int"/>
              </xs:extension>
      </xs:complexContent>
</xs:complexType>

The problem is to define the type for "item" element. If I use
"itemType" as type it the XML does not validated because
"itemType" is abstract. I I use:

<xs:element  name="items">
      <xs:complexType>
              <xs:sequence>
                      <xs:choice>
                              <xs:element name="item" type="itemTypePenColor"/>
                              <xs:element name="item"
type="itemTypePencilSmoothness"/>
                      </xs:choice>
              </xs:sequence>
      </xs:complexType>
</xs:element>

the XML does not validate because the validator assumes that the
"item" type is the first defined (itemTypePenColor). If I
change the declaration of "item"  element then the validator
assumes that the type of "item" is "itemTypePencilSmoothness".

Do you know how to solve this problem?

Thanks in advance.

Pau
Received on Friday, 2 February 2007 18:13:15 GMT

Subscribe to the Stylus Scoop newsletter for helpful XML tips and tutorials.
Email
First Name
Last Name
Company

Download Stylus Studio 6 XML Enterprise Edition

Subscribe in XML format
RSS 2.0
Atom 0.3
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-2007 All Rights Reserved.