W3C home > Mailing lists > Public > xmlschema-dev@w3.org > February 2007

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

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

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

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"/>

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

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

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

<xs:complexType name="itemTypePencil">
              <xs:restriction base="itemType">
                      <xs:attribute name = "name" type = "nameType"

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

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

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

<xs:complexType name="itemTypePen">
              <xs:restriction base="itemType">
                      <xs:attribute name = "name" type = "nameType"

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

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

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:element name="item" type="itemTypePenColor"/>
                              <xs:element name="item"

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.

Received on Friday, 2 February 2007 18:13:15 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 14:56:11 UTC