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:

<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 UTC

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