Search code examples
xpathduplicatesdistinct-values

How to find duplicate / non distinct values with xpath


Given the following example html:

<products>
  <product>
    <sku>10021</sku>
  </product>
  <product>
    <sku>10021</sku>
  </product>
  <product>
    <sku>10022</sku>
  </product>
  <product>
    <sku>10023</sku>
  </product>
  <product>
    <sku>10023</sku>
  </product>
</products>

I know how to find distinct sku values with xpath: distinct-values(//sku), which will output:

10021
10022
10023

But how would I get the ones that are not distinct, so:

10021
10023

I am using xidel, so XPath 3 is fine. But if it can be done somehow with XPath 1, preferably without XSLT, I would very much like to read about that as well.


Solution

  • You can try this one to get sku node with text that equal to text of at least one another sku:

    distinct-values(//sku[.=following::sku])
    

    You can also do it in XPath 1 like this:

    //sku[.=preceding::sku and not(.=following::sku)]