Find duplicated XML Element Names (xPath with variable)

后端 未结 2 989
萌比男神i
萌比男神i 2021-01-27 19:26

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 duplica

相关标签:
2条回答
  • 2021-01-27 19:37

    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
       }
    
    0 讨论(0)
  • 2021-01-27 19:50

    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 讨论(0)
提交回复
热议问题