查找重复的XML元素名称(带有变量的xPath) [英] Find duplicated XML Element Names (xPath with variable)

查看:252
本文介绍了查找重复的XML元素名称(带有变量的xPath)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的JAVA项目中使用XPATH 1.0解析器和CLiXML,我正在尝试设置CLiXML约束规则文件。

I'm using XPATH 1.0 parsers alongside CLiXML in my JAVA project, I'm trying to setup a CLiXML constraint rules file.

我想展示一个如果特定子项下存在重复的元素名称,则会出错。

I would like to show an error if there are duplicate element names under a specific child.

例如

<parentNode version="1">
    <childA version="1">
        <ignoredChild/>
    </childA>
    <childB version="1">
        <ignoredChild/>
    </childB>
    <childC version="4">
        <ignoredChild/>
    </childC>
    <childA version="2">
        <ignoredChild/>
    </childA>
    <childD version="6">
        <ignoredChild/>
    </childD>
</parentNode>

childA出现不止一次,所以我会显示错误。

childA appears more than once, so I would show an error about this.

注意:我只想检查/计算元素名称,而不是内部属性或元素的子元素。

NOTE: I only want to 'check/count' the Element name, not the attributes inside or the children of the element.

我尝试过的.clx规则文件中的代码是:

The code inside my .clx rules file that I've tried is:

<forall var="elem1" in=".//parentNode/*">
    <equal op1="count(.//parentNode/$elem1)" op2="1"/>
</forall>

但这不起作用,我收到错误:

But that doesn't work, I get the error:

Caused by: class org.jaxen.saxpath.XPathSyntaxException: count(.//PLC-Mapping/*/$classCount: 23: Expected one of '.', '..', '@', '*', <QName>

正如我想要的那样用于检查每个子名称并使用子名称运行另一个xPath查询的代码 - 如果计数大于1,那么它应该给出错误。

As I want the code to check each child name and run another xPath query with the name of the child name - if the count is above 1 then it should give an error.

任何想法?

推荐答案

尝试获取具有适当路径表达式的子节点列表,并检查该列表中的重复项:

Just try to get list of subnodes with appropriate path expression and check for duplicates in that list:

   XPathExpression xPathExpression = xPath.compile("//parentNode/*");
   NodeList children = (NodeList) xPathExpression.evaluate(config, XPathConstants.NODESET);

   for (int i = 0; i < children.getLength(); i++) {
   // maintain hashset of clients here and check if element is already there
   }

这篇关于查找重复的XML元素名称(带有变量的xPath)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆