1
votes

I have an XML element, let's say <fruit>, which is restricted by an enumeration so that it can only take the values: apple, orange, watermelon

I also have another field which should also be restricted by an enumeration with the following values: red, orange, green

Is there any way to associate these two elements somehow, so that the only possible combinations to be: apple-red, orange-orange, watermelon-green ? I was trying to find some kind of enumeration in complex types but it seems that XSD doesn't support them.

2

2 Answers

1
votes

No in XSL Schema there is no possibility to do that.

There are two ways you can achieve what you want:

If you really have to stick with XSD-Schema:

  • Using Schematron transformation and defining those rules

If you're not forced to use XSD-Schema by your environment

  • you should really use Relax NG instead.

In Relax NG you have much more possibilitys to express complex behaviour. And on top its much easier to write and understand than XSD Schema.

In Relax NG what you want to express would look like this (you would define your element 3 times in a choice)

<choice>
 <element name="fruit-combination">
  <element name="fruit">
   <value>apple</value>
  <element>
  <element name="colour">
   <value>red</value>
  </element>
 </element>
 <element name="fruit-combination">
  <element name="fruit">
   <value>orange</value>
  <element>
  <element name="colour">
   <value>orange</value>
  </element>
 </element>
 <element name="fruit-combination">
  <element name="fruit">
   <value>watermelon</value>
  <element>
  <element name="colour">
   <value>green</value>
  </element>
 </element>
</choice>

The Relax NG implementation will then choose the right element for you. If you're using a good xml editor and you put your apple element in the file it will even restrict you so that you can only add an colour element with value red and so on.

In Schematron you would just write a rule for your colour element with value red is only valid if there is a fruit element with value apple in it. Schematron itsself is nothing but a special XSL-Transformation which applys some rules to the xml document and outputs a report of the Errors and Warnings in XML Form.

See:

http://relaxng.org/ http://www.schematron.com/ (Specification) http://www.schematron.com/implementation.html (Implementation)

0
votes

This is a well known limitation in XSD 1.0, which is fixed in XSD 1.1 through the mechanisms of Assertions and/or Conditional Type Assignment.

XSD 1.1 is implemented currently in Saxon-EE and in Apache Xerces.