A beautiful and simple solution is not available in the XML schema (but in other languages), but you can actually, by investing in it everything that I can not recommend.
So, if I were you, I would do one of two things:
Quote from the Wiki about XML Schema version 1.1 (candidate recommendation):
Ability to define statements against document content using XPath 2.0 expressions (idea taken from Schematron)
<- This will make the depth easy to determine.
For comments on how to represent the depth of nesting in XMLSchema :
Basically, you can do something like the following (still recommending doing this in code). Then you add attributes, adjust depth, etc. (You can reuse attributes with extension or restrict , but I'm not 100% sure). This method can become quite unpleasant (exponential) if you allow several types of subelements:
<?xml version="1.0" encoding="utf-8" ?> <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://somenamespace.com" xmlns="http://somenamespace.com"> <xs:element name="hierarchy"> <xs:complexType> <xs:sequence> <xs:element name="group" type="GroupTypeDepth0" /> </xs:sequence> <xs:attribute name="name" type="xs:string" /> </xs:complexType> </xs:element> <xs:complexType name="GroupTypeDepth0"> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" name="group" type="GroupTypeDepth1" /> </xs:sequence> </xs:complexType> <xs:complexType name="GroupTypeDepth1"> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" name="group" type="GroupTypeDepth2" /> </xs:sequence> </xs:complexType> <xs:complexType name="GroupTypeDepth2"/> </xs:schema>
Really:
<hierarchy xmlns="http://somenamespace.com"> <group> <group> <group/> </group> </group> </hierarchy>
Invalid:
<hierarchy xmlns="http://somenamespace.com"> <group> <group> <group> <group/> </group> </group> </group> </hierarchy>
source share