XPath: a node search is duplicated n times with a single path expression query

I write some XPath queries and focus on one. Below is an example of the document that 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 get all the employees whose country appears more than twice in the document. I started with a simpler query, namely one that should find duplicates:

<!-- language: lang-xsl -->
doc("emp.xml")//emp[preceding::emp/country=./country or following::emp/country=./country]

although it returns all employees (obviously, Oliver should not be listed among the results).

I am new to XPath and not quite sure if I will get the point concept. qualifier right. I expect that the above query will behave as follows: iterating over the set of emp nodes and for each check if there is an employee with the same country among the nodes that appear above and below the current one in the document.

( GROUP BY) ( ?). , eXide ( eXist-db 2.1) XQuery 3.0 .

+3
3

XPath 2.0

//emp[count(index-of(//country/text(), country/text())) > 2]

index-of country/text() , , , , 2.

+4

XQuery 1.0, , . $src. , :

$src//emp[let $emp-country := country return count($src//data(country)[. = $emp-country]) > 2]

, :

let $all-countries := $src//data(country)
return
    $src//emp[let $emp-country := country return count($all-countries[. = $emp-country]) > 2]
+3

XQuery 3.0 group by, . , :

for $employee in //emp
let $country := $employee/country
group by $country
where count($employee) > 2
return $employee

:

  • - . - eXist DB, "" . BaseX Zorba. , ?
  • You wrote "whose country appears more than two times": This is what I implemented above. Considering your request, perhaps you wanted to "at least twice"? If so, change the offer whereto suit your requirements. Otherwise, the problem in your request is that you can use andinstead or, but this will not indicate the first and last employee for this country.
+2
source

All Articles