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?