I am practising writing some XPath queries and am stuck at one particular. Below is a sample document I am using:
<dept-db>
<dept>
<name>HR</name>
<emp>
<name>John</name>
<country>USA</country>
</emp>
<emp>
<name>Chris</name>
<country>USA</country>
</emp>
</dept>
<dept>
<name>Technology</name>
<emp>
<name>Oliver</name>
<country>UK</country>
</emp>
<emp>
<name>Emily</name>
<country>USA</country>
</emp>
</dept>
</dept-db>
What I want to achieve is to retrieve all employees whose country appears more than twice in the document. I started with a simpler query, namely one which is supposed to find duplicates:
<!-- language: lang-xsl -->
doc("emp.xml")//emp[preceding::emp/country=./country or following::emp/country=./country]
though it returns all the employees (obviously Oliver should not be listed among the results).
I'm new to XPath and am not quite sure if I get the concept of the dot '.' specifier right. I expect the aforementioned query to behave like this: iterate over the set of emp nodes and for each check if there's an employee with the same country among the nodes that appear above and below the current one in the document.
I'd be thankful for an explanation (the application of the dot specifier to perform GROUP BY kind of queries) and help with getting the query to work (unless it is not possible with a single path expression?). If it matters, I'm using eXide (part of eXist-db 2.1) with XQuery 3.0 to perform queries.