使用xquery FLWOR表达式查找多个“where”限制

时间:2011-12-11 22:22:32

标签: xml xpath xquery xpath-2.0 flwor

我正在努力找到在休斯顿工作的员工,但项目所在的部门并不在休斯敦。我试图在this FLWOR表达式示例之后对表达式进行建模,但查询没有返回任何内容,它应该返回结果。

编辑:这是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>
/

3 个答案:

答案 0 :(得分:6)

一个for条款就足够了;否则,你会多次遍历所有员工:

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>

答案 1 :(得分:2)

单个XPath 2.0表达式可以选择所有想要的员工:

/*/employees
     /*[projects/*/@pno
       =
        /*/projects/*
               [plocation eq 'Houston'
              and
                boolean
                (for $dn in @controllingDepartment
                  return
                   /*/departments/*
                       [@dno eq $dn
                      and
                        not(locations/location
                          =
                           'Houston'
                           )
                       ]
                )
               ]/@pnumber
       ]

答案 2 :(得分:1)

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

你的“s”太多了:plocations