Allow / Restrict XML Attribute Attributes Based on Other Attributes

Is it possible to create an XML schema with the following behavior? I have an XML file that looks like this. I would like to either set or restrict the attributes of the object based on the value of Type. For example, if Type = "Bike", I may need to set attributes that apply only to Bike (for example, pedals, frame, etc.). If Type = "Car", I can highlight attributes that apply only to the 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.

+4
source share
4 answers

I understand that this is not something you can do with xsd. In addition, it causes a nightmare. The preferred approach (if possible) is to use:

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

Where car and bike supposedly have a common vehicle root in xsd, but their own specific properties (over inherited).

+4
source

I think you type too much into attributes. Things like pedals and frames should probably be elements.

+2
source

I agree with Marc that what you are trying to do is beyond the scope of the W3C XML Schema. I also agree that the data model is a bit overloaded / ambiguous to be useful. However, if you have no choice in this matter, then perhaps Schematron or Relax-NG can provide a workable solution.

0
source

Yes, you just need to use xsi:type instead of Type for XSD polymorphism (and in the scheme deduce types from the general type):

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

See XML Schema Part 0: Primer for more details. He has an example schema for this in which the USAddress and UKAddress types from Address are produced . Below is an example example using xsi:type .

0
source

Source: https://habr.com/ru/post/1285965/


All Articles