Search code examples
xmlxpathxqueryxpath-2.0flwor

Using xquery FLWOR expressions to find multiple "where" restrictions


I am trying to find employees who work on projects located in Houston but the department the project is housed in is not located in Houston. I was trying to model the expression after this example of FLWOR expressions but the query doesn't return anything and it should return results.

edit: Here is the input.

xquery
let $doc := doc("~path/company.xml")
for $e in $doc//employee,
    $d in $doc//department,
    $p in $doc//projects
where $d/locations[location!="Houston"]
and $p/project[plocations="Houston"]
return <e>{$e/fname}{$e/lname}{$e/address}</e>
/

Solution

  • One for clause is enough; otherwise, you'll iterate over all employees several times:

    let   $doc := doc(...)
    for   $e in $doc//employee
    let   $p := $doc//project[@pnumber = $e/projects/worksOn/@pno]
    where $p[plocation = 'Houston']
      and $doc//department[@dno = $p/@controllingDepartment]
                          [not(locations/location = 'Houston')]
    return <e>{ $e/fname }{ $e/lname }{ $e/address }</e>