I have 3 XML files as below and I would want to join all this 3 documents together so that i can perform an aggregation function to find out how many sales have been made for a particular classification name. However seems like the code i wrote has a problem. Please, guide me on how can I perform aggregation function on the three documents.
ClassDescription.XML
<classification name="Electronic">
<Description>electronic devices that requires electric</Description>
</classification>
<classification name="SoftToy">
<Description>Fluffy toys that kids like</Description>
</classification>
ToyClassification.XML
<toy toyID="11">
<name>Doll</name>
<classification>SoftToy</classification>
</toy>
<toy toyID="22">
<name>Xbox</name>
<classification>Electronic</classification>
</toy>
<toy toyID="33">
<name>PS3</name>
<classification>Electronic</classification>
</toy>
ToySale.XML
<toySale companyID="1" toyID="11" >
<amount>15</amount>
</toySale>
<toySale companyID="3" toyID="11" >
<amount>12</amount>
</toySale>
<toySale companyID="1" toyID="22" >
<amount>3</amount>
</toySale>
<toySale companyID="2" toyID="33" >
<amount>7</amount>
</toySale>
<ClassList>
<classification name="SoftToy">
<totalSale>4</totalSale>
</classification>
<classification name="Electronic">
<totalSale>3</totalSale>
</classification>
</ClassList>
Below was the code that i have but seems like its not working.May i know what is the correct xquery for this to work?
for $class in (ClassDescription.XML)//classification/@name
for $toyClass in (ToyClassification.XML)//toy/@toyID
for $sale in (ToySale.XML)//toySale/@toyID
let $sum := (ToySale.XML)//toySale[@toyID = $toyClass]
where $sale=$toyClass and $class=$toyClass/../name
order by sum($sum/amount)
return <ClassList>{$class}</ClassList>
This query:
when the three XML documents reside in the respective files:
c:/temp/delete/classDescription.xml:
c:/temp/delete/toyClassification.xml:
c:/temp/delete/toySale.xml:
produces the wanted, correct result:
Do note, that the CompanyID data isn’t used at all in the two other tables.