简体   繁体   中英

Using XQuery to return multiple attributes using different conditions

I am new to XQuery and I am trying to figure out something really simple but I seem to be failing miserably. I have an XML file of Employees in a Company which looks like this:

<dataroot>


<employee>


    <fname>John</fname>


    <minit>B</minit>


    <lname>Smith</lname>


    <ssn>123456789</ssn>


    <bdate>1965-01-09</bdate>


    <address>731 Fondren, Houston, TX</address>


    <sex>M</sex>


    <salary>30000</salary>


    <superssn>333445555</superssn>


    <dno>5</dno>


</employee>


<employee>


    <fname>Franklin</fname>


    <minit>T</minit>


    <lname>Wong</lname>


    <ssn>333445555</ssn>


    <bdate>1955-12-08</bdate>


    <address>638 Voss, Houston, TX</address>


    <sex>M</sex>


    <salary>40000</salary>


    <superssn>888665555</superssn>


    <dno>5</dno>


</employee>
...etc

And that goes on for all the Employees. I also have a Dependents XML file which looks like this:

<dependent>


    <essn>123456789</essn>


    <dependent_name>Alice</dependent_name>


    <sex>F</sex>


    <bdate>1988-12-30</bdate>


    <relationship>daughter</relationship>


</dependent>


<dependent>


    <essn>123456789</essn>


    <dependent_name>Elizabeth</dependent_name>


    <sex>F</sex>


    <bdate>1967-05-05</bdate>


    <relationship>spouse</relationship>


</dependent>
...and so on

My problem is that I'm trying to run an XQuery that returns the name of the dependent, first name and last name of the employee related to that dependent AND the first name and last name of that employee's manager. I'm having trouble when it comes to accessing the managers information seeing as the manager's ssn has to be compared to the employee's ssn to get the name of the manager (both regular employees and managers are part of the employee file).

This is what I have so far:

<results>
{
    for $e in doc("../company/employee.xml")//employee,
        $m in doc("../company/employee.xml")//employee
        $d in doc("../company/dependent.xml")//dependent
    return
        <ans
            dname="{ $d/dependent_name }"
            emp fname="{ $e/fname }"
            emp lname="{ $e/lname }"
            mgr fname="{ } (: *this is where I get lost* :)
        />
}
</results>

So I guess my specific question is: Seeing as I can access each Employee from the Employee.xml and each Dependent from the Dependent.xml files WITHOUT using a conditional (WHERE $e/superssn = $m/essn) how can I return all those values since only one of the returned results requires a conditional statement?

I don't exactly get the your actual question and why you would want to not aa condition, but it seems to me because you are missing or mixing up some concepts in your XQuery.

First of all, if you write

for $e in doc("../company/employee.xml")//employee,
    $m in doc("../company/employee.xml")//employee
    $d in doc("../company/dependent.xml")//dependent

this are three interleaved for loops, which seems totally unnecessary. If I understood correctly you want to get an employee and all of his/her dependents and the employees manager. You should use let for this

for $e in doc("../company/employee.xml")/dataroot/employee
for $d in doc("../company/dependent.xml")//dependent[essn = $e/ssn]
let $m in doc("../company/employee.xml")/dataroot/employee[ssn = $e/superssn]
return
    <ans
        dname="{ $d/dependent_name }"
        emp fname="{ $e/fname }"
        emp lname="{ $e/lname }"
        mgr fname="{ $m/fname }
    />

I also replaced // with a concrete step. You should try to avoid // wherever possible as it has to search the whole tree and this can be quite large.

And please use more meaningful variable names than $m . $manager is much more easy to read and it doesn't cost you a thing.

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