I’ve always understood XMLSchemas and DTDs to be equivalent but that the latter is more cumbersome to use when modeling complex relationships (like inheritance).
Recently I wanted to build a schema to validate documents that have a structure like this:
<data>
<array>
<int></int>
<int></int>
</array>
</array>
<float></float>
<float></float>
</array>
<int><int>
<float></float>
</data>
The elements inside < data > can appear in any order and each is of cardinality 0..*
Using XMLSchema, if I define a complex type using < xs:all > I can have the elements out of order but the maximum cardinality is 1. < xs:sequence > and < xs:choice > are the other obvious candidates but they’re more restrictive than what I want.
Then I noticed that a DTD seems to be able to achieve this like so:
<!ELEMENT data (array | float | int)*>
Is there any way to build an equivalent schema or do I have to use DTDs here?
I thought I’d come back to this as the previous answer is incorrect.
Infact, one can solve the original problem using XML Schema.
The correct approach is to define a group element which is contains a choice between all the various options (ints, floats, arrays) and each one has cardinality 0..*.
From here, it remains to reference the group in a complexType definition and set the cardinality of the group as 0..*
et voila. a bit verbose (especially compared to RelaxNG’s syntax) but the upside is that XML Schema is much better supported. I had crafted a RelaxNG based parser to solve the original problem but the available validators (like JING) are rather more clunky than using the XML Schema based tools that ship with Java et al.