Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - XML Schema: inheritance with variable order of childs >Thread Next - Re: XML Schema: inheritance with variable order of childs Re: XML Schema: inheritance with variable order of childsTo: NULL Date: 11/2/2007 3:28:00 PM Sven <sven@a...> wrote in <1194007305.931018.87630@5...>: > <TextItem> > <Name>Temperature</Name> > <Content>27°C</Content> > </TextItem> > <TextItem> > <Content>cloudy</Content> > <Name>Sky</Name> > </TextItem> > > With a variable sequence order for Name and Content I can > define this as: > > <xs:complexType name="TextItemType"> > <xs:sequence maxOccurs="unbounded" minOccurs="1"> > <xs:choice> > <xs:element name="Name" type="xs:string" /> > <xs:element name="Content" type="xs:string" /> > </xs:choice> > </xs:sequence> > </xs:complexType> Which is too lax. > As I want define different Item types I define a base type > Item and derive the special content types: > > <xs:complexType name="ItemType"> > <xs:sequence> > <xs:element maxOccurs="1" minOccurs="1" name="Name" > type="xs:string" /> > </xs:sequence> > </xs:complexType> > > <xs:complexType name="TextItemType"> > <xs:complexContent> > <xs:extension base="ItemType"> > <xs:sequence> > <xs:element maxOccurs="1" minOccurs="1" > name="Content" > type="xs:string" /> > </xs:sequence> > </xs:extension> > </xs:complexContent> > </xs:complexType> > > But with this schema, the order of "Name" and "Content" is > relevant. Is there a way to define this in a way that this > order is variable? I believe the answer is no, but you'd have to hire a language lawyer to quote chapter and verse on it. Anyway, that's beside the point, because you cannot differentiate element types based on element content. xsi:type is the only way around it and it's, well, clunky. See newsgroup archives, this is one of the most commonly asked questions about XML Schemata. The standard recommendations are: 1. Stop wanting that. 2. Use a more powerful schema definition language. 3. Validate on application side, not on parser side. 4. Design a well-structured document: <temperature scale="celsius">27</temperature> <sky>cloudy</sky> Another interesting way of dealing with modestly crippled XML documents is transforming them into something sane using XSLT. Make the transformation scream and swear if it runs into something that shouldn't be there, and you're golden. -- "I can't help but wonder if you... don't know a hell of a lot more about practically every subject than Solomon ever did." | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
