Limit depth of nested elements in XSD

Is it possible with an XML schema to limit the depth of child elements nested in the parent?

In this context, I collect alarms from the management system, and I want to provide an XML document that allows end users to define some rules for filtering alarms in folders in the user interface. I want to limit the depth of the subfolders to 3 so that the end user cannot nest hundreds of levels in depth - since filtering by so many levels will lead to the crash of the application in the end.

I could write code to handle it, but it seems advisable to define this in the circuit, if possible.

For example, this would be good:

<group name="Folder 1"> <group name="Folder 2"> <group name="Folder 3"> <group name="Folder 4"> </group> </group> </group> </group> 

This will not be correct since folder 5 is too deep.

 <group name="Folder 1"> <group name="Folder 2"> <group name="Folder 3"> <group name="Folder 4"> <group name="Folder 5"> </group> </group> </group> </group> </group> 

My layout looks like this, but does not limit the depth of the fragment above.

 <?xml version="1.0" encoding="utf-8" ?> <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="hierarchy"> <xs:complexType> <xs:sequence> <xs:element name="group" type="GroupType" /> </xs:sequence> <xs:attribute name="name" type="xs:string" /> </xs:complexType> </xs:element> <xs:complexType name="GroupType"> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" name="group" type="GroupType" /> </xs:sequence> <xs:attribute name="name" type="xs:string" use="required" /> <xs:attribute name="filterOn" type="xs:string" use="optional" /> <xs:attribute name="operator" type="xs:string" use="optional" /> <xs:attribute name="value" type="xs:string" use="optional" /> </xs:complexType> </xs:schema> 

Any pointers are much appreciated!

+4
source share
1 answer

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> 
+4
source

All Articles