2011-12-11 53 views
1

我試圖找到沒有使用xquery的依賴者的員工,但是當我運行查詢時,響應是我知道的信息不正確。以下是我正在使用的查詢:使用xquery查找沒有家屬的員工

xquery 
let $d:=doc("/public/book/company.xml") 
let $e:=$d/companyDB/employees/employee 
for $name in $d/companyDB/employees/employee 
where count($e/dependent)<1 
return <e>{$e/fname}{$e/lname}</e> 
/

我希望這隻會返回少於1個依賴項的員工。所以給出的例子數據,它只會返回James Borg。但它會返回所有人。爲什麼是這樣?

編輯:我在Oracle 11g中使用sql plus中的xquery命令。

這裏是xml文件的樣本:

<companyDB xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="company.xsd"> 
<departments>...</departments> 
    <employees> 
    <employee ssn="333445555" worksFor="5" supervisor="888665555" manages="5"> 
     <fname>Franklin</fname> 
     <minit>T</minit> 
     <lname>Wong</lname> 
     <dob>08-DEC-45</dob> 
     <address>638 Voss, Houston, TX</address> 
     <sex>M</sex> 
     <salary>40000</salary> 
     <dependents> 
      <dependent>...</dependent> 
      <dependent>...</dependent> 
      <dependent>...</dependent> 
     </dependents> 
     <supervisees essns="123456789 666884444 453453453"/> 
     <projects>...</projects> 
    </employee> 
    <employee ssn="888665555" worksFor="1" manages="1"> 
     <fname>James</fname> 
     <minit>E</minit> 
     <lname>Borg</lname> 
     <dob>10-NOV-27</dob> 
     <address>450 Stone, Houston, TX</address> 
     <sex>M</sex> 
     <salary>55000</salary> 
     <supervisees essns="333445555 987654321"/> 
     <projects>...</projects> 
    </employee> 
</employees> 
</companyDB> 

回答

2

一個簡單的XPath表達式產生連在一起的名稱的通緝序列:

doc("/public/book/company.xml") 
     /*/employees/employee[not(dependents/dependent)] 
         /concat(fname, lname) 
1
let $xml := <companyDB> 
    <employees> 
    <employee ssn="333445555" worksFor="5" supervisor="888665555" manages="5"> 
     <fname>Franklin</fname> 
     <minit>T</minit> 
     <lname>Wong</lname> 
     <dob>08-DEC-45</dob> 
     <address>638 Voss, Houston, TX</address> 
     <sex>M</sex> 
     <salary>40000</salary> 
     <dependents> 
      <dependent>...</dependent> 
      <dependent>...</dependent> 
      <dependent>...</dependent> 
     </dependents> 
     <supervisees essns="123456789 666884444 453453453"/> 
     <projects>...</projects> 
    </employee> 
    <employee ssn="888665555" worksFor="1" manages="1"> 
     <fname>James</fname> 
     <minit>E</minit> 
     <lname>Borg</lname> 
     <dob>10-NOV-27</dob> 
     <address>450 Stone, Houston, TX</address> 
     <sex>M</sex> 
     <salary>55000</salary> 
     <supervisees essns="333445555 987654321"/> 
     <projects>...</projects> 
    </employee> 
</employees> 
</companyDB> 

return 
$xml/employees/employee[not(dependents/dependent)] 

使用謂詞可以更容易(更快)。

+0

我嘗試這樣做,也沒有工作,爲什麼?因爲doc()返回一個文檔{},所以你需要爲'$ xml:= doc(「/ public/book/company.xml」) 返回$ xml/employees/employee [不是(依賴/依賴)] – mnky9800n

+0

修改xpath,概要類似$ xml/companyDB/employees/employee [not(dependents/dependent)] –