Reputation: 249
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.
Upvotes: 4
Views: 622
Reputation: 12187
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
.
Upvotes: 0
Reputation: 119806
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.
Upvotes: 0
Reputation: 6950
I think you are cramming too many things into attributes. Things like Pedals and Frame should probably be elements.
Upvotes: 2
Reputation: 1062494
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).
Upvotes: 4