tags:

views:

150

answers:

4

Is it possible to create an XML schema with the following behavior? I have an XML file that looks like the following. I'd like to either expose or restrict attributes of Object based on the value of Type. For example, if Type="Bike" I might want to expose attributes related only to Bike (i.e. Pedals, Frame, etc). If the Type="Car", I might want to expose attributes that are only relevant to Car (i.e. Make, Model, Miles, etc).

<Objects>
  <Object Type="Bike" Pedals="XXX" Frame="XXX" />
  <Object Type="Car" Make="XXX" Model="XXX" Miles="XXX" />
</Objects>

Thanks in advance for any help. Let me know if you have any questions.

+3  A: 

My understanding is that this is beyond what you can do with xsd. Besides, it makes querying a nightmare. The preferred approach (if it is possible) is to use:

<vehicles>
  <bike pedals="XXX" frame="XXX" />
  <car make="XXX" model="XXX" miles="XXX" />
</vehicles>

Where car and bike presumably have a common root vehicle in the xsd, but their own specific properties (on top of the inherited ones).

Marc Gravell
I'm not sure if there's any technical resons why this wouldn't be possible - this is absolutely the correct approach and has even been specifically addressed by the W3C though I'm struggling to find the document.
annakata
by "if it is possible", I meant "for your scenario" ;-p
Marc Gravell
+2  A: 

I think you are cramming too many things into attributes. Things like Pedals and Frame should probably be elements.

dss539
+1 exactly so
annakata
A: 

I agree with Marc that what you're trying to do is outside of the capabilities of W3C XML-Schema. I'd also agree that you're data model is just a bit overloaded/ambiguous to be useful. However, if you have no choice in the matter then perhaps either Schematron or Relax-NG could provide a workable solution.

Kev
A: 

Yes, you just have to use xsi:type instead of Type for XSD polymorphism (and in the schema, derive the types from a common type):

<Objects>
  <Object xsi:type="Bike" Pedals="XXX" Frame="XXX" />
  <Object xsi:type="Car" Make="XXX" Model="XXX" Miles="XXX" />
</Objects>

For details, see the XML Schema Part 0: Primer. It has an example schema for this, in which USAddress and UKAddress types are derived from Address. Further down is an example instance using xsi:type.

13ren