使用xpath

时间:2015-10-18 17:17:59

标签: xml xpath nested depth

在xml模式中,允许无限制地嵌套< item> s。在无序的项目列表中(随机列表的< randlist>)。这些项目可能包含可以使用< item>列出的子项目。作为另一个列表元素中的子元素,例如< randlist>或< seqlist>对于有序列表。

现在,我想在文档中检测超过3个嵌套深度级别的嵌套,以对其应用一些约束。使用xpath这是应该允许无条件的:

randlist //项目

randlist // //项项目

randlist // //项项目//项目

应禁止具有超过3个项目嵌套深度级别的randlists,例如

randlist // //项项目// //项项目

如何使用xpath来表达表达第三级以外元素嵌套的表达式?

提前谢谢

对不起人!所以这里的例子就是

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

我需要检测超过3个级别的项目列表,即第四级和更多级别。我需要像randlist [count(nesting(item))&gt; 3],如果在xpath中有像“嵌套”这样的函数。

3 个答案:

答案 0 :(得分:2)

您自己提供的表达方式有什么问题:

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

这将选择具有三个或更多项目祖先的任何项目。

效率更高可能是

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

但这取决于您使用的XPath处理器。

答案 1 :(得分:1)

从您的问题来看,知道完全您想要的输出是什么有点棘手,但我认为以下建议之一应该足够,或者帮助您实现目标。

作为一般示例,您可以通过将一组节点/元素传递给count XPath函数来计算超过特定嵌套级别的元素数。例如:

<强> XML

<randlist>
    <item>
        <content>Example Content</content>
        <subitem>
          <p>Content</p>
          <p2>More Content</p2>
        </subitem>
    </item>
</randlist>

<强>的XPath

count(randlist/item//*)

这将计算作为randlist元素的直接子元素的item元素的后代元素的数量。双斜杠//表示XPath搜索后代,*运算符是匹配任何元素的通配符,不管元素名称如何。因此它将返回 4 - 因为有4个元素:contentsubitempp2

在您的情况下,我认为您希望检测位于randlist三个嵌套级别内的seqlistitem元素。如果要检测此事件,可以使用:

count(randlist//item//item//item//randlist|randlist//item//item//item//seqlist)

|运算符计算两个集合的并集。如果要检测位于item三个嵌套级别内的任何元素的出现次数,则应再次使用通配符运算符*

count(randlist//item//item//item//*)

答案 2 :(得分:1)

假设randlist始终是XML文档的根元素,这是一个可能的XPath表达式,用于检查它是否包含3个以上级别的嵌套item元素:

randlist[.//item[count(ancestor::item)>=3]]