Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
/
like image 268
mnky9800n Avatar asked Dec 11 '11 22:12

mnky9800n


3 Answers

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>
like image 64
Christian Grün Avatar answered Oct 18 '22 19:10

Christian Grün


A single XPath 2.0 expression can select all wanted employees:

/*/employees
     /*[projects/*/@pno
       =
        /*/projects/*
               [plocation eq 'Houston'
              and
                boolean
                (for $dn in @controllingDepartment
                  return
                   /*/departments/*
                       [@dno eq $dn
                      and
                        not(locations/location
                          =
                           'Houston'
                           )
                       ]
                )
               ]/@pnumber
       ]
like image 42
Dimitre Novatchev Avatar answered Oct 18 '22 19:10

Dimitre Novatchev


Typo: and $p/project[plocation="Houston"]

You had an "s" too much: was plocations.

like image 1
Jens Erat Avatar answered Oct 18 '22 21:10

Jens Erat