Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1039521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:06:36+00:00 2026-05-16T15:06:36+00:00

Is it possible with XML schema to restrict the depth of child elements nested

  • 0

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

The context here is I collect alarms from a management system and I want to provide a XML document which allows the end user define some rules in order to filter the alarms into folders in the UI. I want to restrict the depth of nested folders to 3 so an end user can’t nest hundreds of levels deep – as filtering to so many levels would crash the application eventually.

I could write some code to handle this, but it seems appropriate to define this in the schema if its possible.

For example, this would be fine:

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

This would be invalid, as 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 schema looks like this, but its not restricting the depth for the snippet 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 much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:06:37+00:00Added an answer on May 16, 2026 at 3:06 pm

    A pretty and simple solution is not available in XML Schema (but is in other languages) but you can actually to it, by nesting the whole thing yourself which I can’t recommend.

    So, if I were you, I would do one of two things:

    1. Pick an alternative to XML Schema (or an extension) See: http://en.wikipedia.org/wiki/XML_schema#XML_schema_languages (take a look at RELAX and Schematron)
    2. Do the depth checking in your application and reject it if depth > 3 and make the behavior clear in the documentation for your XML Schema and application.

    Quote from Wiki about XML Schema version 1.1 (candidate recommendation):

    The ability to define assertions against the document content by means of XPath 2.0 expressions (an idea borrowed from Schematron)

    <- This will do the depth pretty easy to define.

    For comment on how to represent nesting depth in XMLSchema:

    Basically you can do something like the following (still recommending doing it in code). You then add attributes, adjusting depth etc. (you may be able to reuse attributes with extension or restrict but I’m not 100% sure). This method can get pretty nasty (exponential) if you allow multiple kinds of sub-elements:

    <?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>
    

    Valid:

    <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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.