Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xml-dev] An element with more than one possible type

From: "Pete Cordell" <petexmldev@--------------.--->
To: "pau carre" <pau.carre@-----.--->, <xml-dev@-----.---.--->
Date: 2/2/2007 12:07:00 PM
Hi Pau,

It's not possible to do this exactly how you want.  You could use schemas 
xsi:type mechanism to approximate it.  In this case your last construct 
would be defined as:

<xs:element  name="items">
        <xs:complexType>
                <xs:sequence>
                    <xs:element name="item" type="itemType"/>
                </xs:sequence>
        </xs:complexType>
</xs:element>

and your XML instance would become:

<items>
   <item xsi:type = "itemTypeWaterVolume" volume ="22"/>
   <item xsi:type = "itemTypeMeatWeight" weight = "10" />
</items>

Although you could use simpler names if you like.  (Also your restrictions 
to itemTypeMeat and so on would not be required.)

You could also aim for something like:

<items>
   <itemTypeWaterVolume volume ="22"/>
   <itemTypeMeatWeight weight = "10" />
</items>

Other than that you would need something like Schematron (or wait for 
assertions to appear in XSD1.1!)

HTH,

Pete.
--
=============================================
Pete Cordell
Tech-Know-Ware Ltd
for XML to C++ data binding visit
http://www.tech-know-ware.com/lmx
(or http://www.xml2cpp.com)
=============================================

Original Message From: "pau carre"


>I am trying to code a XML Schema (XSD file) for a web service in such a 
>way:
>
> <items>
>   <item name = "water" volume ="22">
>   <item name = "meat" weight = "10" >
> </items>
>
> It is to say, if name is "water" , then use the "volume" attribute.
> When name is "meat" then use "weight" 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="water"/>
>                <xs:enumeration value="meat"/>
>        </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 "meat":
>
> <xs:complexType name="itemTypeWaterVolume">
>        <xs:complexContent>
>                <xs:restriction base="itemType">
>                        <xs:attribute name = "name" type = "nameType"
> fixed="meat"/>
>                </xs:restriction>
>        </xs:complexContent>
> </xs:complexType>
>
> ... and finally I added an attribute "weight":
>
> <xs:complexType name="itemTypeMeatWeight">
>        <xs:complexContent>
>                <xs:extension base="itemTypeMeat">
>                        <xs:attribute name = "weight" type = "xs:int"/>
>                </xs:extension>
>        </xs:complexContent>
> </xs:complexType>
>
> For the "water" is the same as "meat". First I restricted the "name"
> attribute to "water":
>
> <xs:complexType name="itemTypeWater">
>        <xs:complexContent>
>                <xs:restriction base="itemType">
>                        <xs:attribute name = "name" type = "nameType"
> fixed="water"/>
>                </xs:restriction>
>        </xs:complexContent>
> </xs:complexType>
>
> ... and finally I added the "volume" attribute
>
> <xs:complexType name="itemTypeWaterVolume">
>        <xs:complexContent>
>                <xs:extension base="itemTypeWater">
>                        <xs:attribute name = "volume" 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="itemTypeWaterVolume"/>
>                                <xs:element name="item"
> type="itemTypeMeatWeight"/>
>                        </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 (itemTypeWaterVolume). If I
> change the declaration of "item"  element then the validator
> assumes that the type of "item" is "itemTypeMeatWeight".
>
> Do you know how to solve this problem?
>
> Thanks in advance.
>
> Pau
>
> _______________________________________________________________________
>
> XML-DEV is a publicly archived, unmoderated list hosted by OASIS
> to support XML implementation and development. To minimize
> spam in the archives, you must subscribe before posting.
>
> [Un]Subscribe/change address: http://www.oasis-open.org/mlmanage/
> Or unsubscribe: xml-dev-unsubscribe@l...
> subscribe: xml-dev-subscribe@l...
> List archive: http://lists.xml.org/archives/xml-dev/
> List Guidelines: http://www.oasis-open.org/maillists/guidelines.php
>
>


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent