Say that I have a PetCage element that should contains one and only one AnimalElement type like so:
<PetCage>
<Cat Name="Kitty">
</PetCage>
<PetCage>
<Dog Name="Buddy">
</PetCage>
Both Cat and Dog are of AnimanlElement type. How can I express said rule in XSD?
What you require is a choice element in your schema. It’ll allow you to express that only one from a set of elements may appear.
EDIT: you’ll need to specify a name for the element, either directly via
nameor in reference to an element defined elsewhere viaref.Your only alternative as far as I know is to have something like this:
This would validate XML files of a form like this:
Unfortunately this leaves you tied to that
animalelement name. I doubt that XML Schema allows you to specify a specific type and lets you use a wildcard for the name. XML Schema validates based on element names and, if the element is defined with an abstract type, thexsi:typeattribute. There is anxs:anydeclaration, but that can’t be tied to any specific type. The following will validate as best as possible, but still allows any element defined under the schema root to be used:It would validate this correctly:
… but it wouldn’t stop you from putting a PetCage in a PetCage.
I’m hoping someone with good knowledge of the XML Schema spec can confirm that this is correct or, preferably, offer the desired solution.