[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] 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: <products> <product name = "water" volume ="22"> <product name = "meat" weight = "10" > </products> 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 "product" element: <xs:complexType name="productType" abstract = "true"> <xs:attribute name = "name" type = "nameType"/> </xs:complexType> I restricted the "name" attribute to "meat": <xs:complexType name="productTypeMeat"> <xs:complexContent> <xs:restriction base="productType"> <xs:attribute name = "name" type = "nameType" fixed="meat"/> </xs:restriction> </xs:complexContent> </xs:complexType> ... and finally I added an attribute "weight": <xs:complexType name="productTypeMeatWeight"> <xs:complexContent> <xs:extension base="productTypeMeat"> <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="productTypeWater"> <xs:complexContent> <xs:restriction base="productType"> <xs:attribute name = "name" type = "nameType" fixed="water"/> </xs:restriction> </xs:complexContent> </xs:complexType> ... and finally I added the "volume" attribute <xs:complexType name="productTypeWaterVolume"> <xs:complexContent> <xs:extension base="productTypeWater"> <xs:attribute name = "volume" type = "xs:int"/> </xs:extension> </xs:complexContent> </xs:complexType> The problem is to define the type for "product" element. If I use "productType" as type it the XML does not validated because "productType" is abstract. I I use: <xs:element name="products"> <xs:complexType> <xs:sequence> <xs:choice> <xs:element name="product" type="productTypeWaterVolume"/> <xs:element name="product" type="productTypeMeatWeight"/> </xs:choice> </xs:sequence> </xs:complexType> </xs:element> the XML does not validate because the validator assumes that the "product" type is the first defined (productTypeWaterVolume). If I change the declaration of "product" element then the validator assumes that the type of "product" is "productTypeMeatWeight". Do you know how to solve this problem? Thanks in advance. Pau [Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] |
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|