|
[XML-DEV Mailing List Archive Home]
[By Thread]
[By Date]
[Recent Entries]
[Reply To This Message]
Re: An element with more than one possible type
- From: "Radu Cernuta" <radu.cernuta@g...>
- To: "pau carre" <pau.carre@g...>
- Date: Fri, 2 Feb 2007 13:48:19 +0100
Hello,
XML Schema has something called the Element Declarations
Consistent rule that forbids two or more element declarations
within the same scope, with the same name and target namespace to
have different types. The construct:
<xs:choice> <xs:element
name="item" type="itemTypeWaterVolume"/>
<xs:element name="item"
type="itemTypeMeatWeight"/> </xs:choice>
crosses this rule.
Apart from what Pete said just said,
there are no solution for your problem in XML Schema. But you could
try to look also at RELAX NG. It can handle your problem elegantly:
<rng:grammar xmlns:rng="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"> <rng:start> <rng:element name="items"> <rng:ref name="itemsContent"/> </rng:element> </rng:start>
<rng:define name="itemsContent"> <rng:element name="item"> <rng:ref name="itemWaterContent"></rng:ref> </rng:element>
<rng:element name="item"> <rng:ref name="itemMeatContent"></rng:ref> </rng:element> </rng:define> <rng:define name="itemWaterContent">
<rng:attribute name="name"> <rng:value>water</rng:value> </rng:attribute> <rng:attribute name="volume"> <rng:data type="int"></rng:data>
</rng:attribute> </rng:define> <rng:define name="itemMeatContent"> <rng:attribute name="name"> <rng:value>meat</rng:value>
</rng:attribute> <rng:attribute name="weight"> <rng:data type="int"></rng:data> </rng:attribute> </rng:define>
</rng:grammar>
I guess this is more or less what you
were trying to do in the XML Schema, if only the rules would allow
it.
Greetings,
Radu Cernuta
2007/2/2, pau carre <pau.carre@g...>:
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="itemTypeMeat">
<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

[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!
Cast Your Vote
We need your help – Vote for DataDirect XML Products!
Winners and finalists announced at SOA World Conference in November.
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
| RSS 2.0 |
 |
| Atom 0.3 |
 |
| |
Stylus Studio has published XML-DEV in RSS and ATOM formats,
enabling users to easily subcribe to the list from their preferred news reader application.
|
Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website.
they were not included by the author in the initial post. To view the content without the Sponsor Links please
click here.
|
|