使用xquery查找没有家属的员工

时间:2011-12-11 07:11:46

标签: xml 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。但它会让所有人回归。这是为什么?

编辑:我在sql plus中使用xquery命令在Oracle 11g上。

以下是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 个答案:

答案 0 :(得分:2)

一个简单的XPath表达式生成所需的连接名称序列:

doc("/public/book/company.xml")
       /*/employees/employee[not(dependents/dependent)]
                       /concat(fname, lname)

答案 1 :(得分: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)]

使用谓词可以更容易(也更快)。