开发者

Find duplicated XML Element Names (xPath with variable)

开发者 https://www.devze.com 2023-03-18 21:03 出处:网络
I\'m using XPATH 1.0 parsers alongside CLiXML in my JAVA project, I\'m trying to setup a CLiXML constraint rules file.

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.

For example

<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 appears more than once, so I would show an error about this.

NOTE: I only w开发者_如何转开发ant to 'check/count' the Element name, not the attributes inside or the children of the element.

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>

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.

Any ideas?


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
   }


This cannot be done with a single XPath 1.0 expression (see this similar question I answered today).

Here is a single XPath 2.0 expression (in case you can use XPath 2.0):

/*/*[(for $n in name() 
      return count(/*/*[name()=$n]) 
      )
     >1
     ]

This selects all elements that are children of the top element of the XML document and that occur more than once.

0

精彩评论

暂无评论...
验证码 换一张
取 消