简体   繁体   中英

using xquery to find employees with no dependents

I am attempting to find employees with no dependents using xquery but when I run the query the response is information that I know is incorrect. The following is the query I am using:

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>
/

I expect this to return only employees with fewer than 1 dependent. So given the example data it would only return James Borg . However it returns everyone. Why is this?

edit: I am using the xquery command in sql plus on Oracle 11g.

Here is a sample of the xml file:

<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>

A simple XPath expression produces the wanted sequence of concatenated names:

doc("/public/book/company.xml")
       /*/employees/employee[not(dependents/dependent)]
                       /concat(fname, lname)
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)]

using predicates can be easier (and faster).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM