4
votes

I'm going to guess that this functionality doesn't exist, but I thought I'd give it one last shot.

I've got an XML structure I'm trying to validate (I have no control over this structure).

In this structure I can have multiple "Condition" tags, each tag has an attribute "type" and then the remaining attributes in that tag is dependent on what that type is. So if for example it looked like so:

<Condition type="LessThan" maxAmount="3">
   <Effect type="Add" value="2" />
</Condition>
<Condition type="GreaterThan" minAmount="1">
   <Effect type="Transform" action="Skew" value="4"  />
   <Effect type="Transform" action="Stretch" value="3" />
</Condition>
<Condition type="Range" minAmount="1" maxAmount="3">
   <Effect type="YouGetTheDrift" />
</Condition>

Then if it's type LessThan, it only needs a maxAmount tag, etc.

There is no way to make attributes required/optional/prohibited base on the value of a previous attribute, hey?

I tried to create a choice with multiple Condition tags, with fixed values of type and custom attribute sets per type, but it complained about the tags having the same name.

2

2 Answers

4
votes

The name for the feature you are describing is "co-occurrence constraints", and they aren't supported in XSD 1.0.

You can do this in XSD 1.1 using the "conditional type assignment" syntax, where the type of an element depends on the values of its attributes; you can also do it using assertions. XSD 1.1 is now a final W3C Recommendation and is implemented in Xerces and Saxon.

1
votes

You are right, this functionality doesn't exist.

It can be done in Schematron, but I don't know if it fits your project.