Search code examples
xmlxpathnesteddepth

Expression for nesting count of elements using xpath


in an xml schema nesting of <item>s is allowed unbounded e.g. in an unordered list of items (which is <randlist> for random list). These items may contain subitems that may be sublisted using <item> as a subelement within another list element such as <randlist> or <seqlist> for ordered lists.

Now I want to detect a nesting of more then 3 nesting depth levels in a document to apply some constraints on it. Using xpath this is what should be allowed unconditioned:

randlist//item

or

randlist//item//item

or

randlist//item//item//item

but

randlists with more then 3 item nesting depth levels should be prohibited, e.g.

randlist//item//item//item//item

How I can use xpath to formulate an expression that expresses a nesting of elements beyond the third level?

thank in advance

sorry people! so here the example goes

<randlist> <!-- first level (not nested at all): allowed -->
    <item>
        This is the first item of an unordered enumeration of items that are prosa altogether.
    </item>
    <item>
        <randlist> <!-- second level (nested): allowed -->
            <item>
                This is the first item of an unordered enumeration of items that are prosa altogether.
            </item>
            <item>
                Another item with some information in the nested unordered list.
            </item>
            <item>
                <seqlist> <!-- third level (double nested): allowed -->
                    <item>
                        This is the first item of an ordered enumeration of items (it may be shown with the number 1).
                    </item>
                    <item>
                        This is the second item of an ordered enumeration of items (it may be shown with the number 2).
                        <randlist> <!-- fourth level (triple nested): should be prohibited -->
                            <item>
                                This is the first item of an unordered enumeration of items.
                            </item>
                            <item>
                                This is the second item of an unordered enumeration of items.
                            </item>
                        </randlist>
                    </item>
                    <item>
                        This is the third item of an ordered enumeration of items (it may be shown with the number 3).
                    </item>
                </seqlist>
            </item>
        </randlist>
    </item>
</randlist>

I need to detect more then 3 levels of item lists, i.e. the fourth level and more. I need something like e.g. randlist[count(nesting(item))>3], if there were a function like "nesting" in xpath.


Solution

  • What's wrong with the expression that you provided yourself:

    randlist//item//item//item//item
    

    This will select any item that has three or more item ancestors.

    More efficient might be

    randlist//item[count(ancestor::item) > 2]
    

    but that depends on the XPath processor you are using.