我有3个XML文件,如下所示,我想将所有这3个文档连接在一起,以便我可以执行聚合功能,以了解特定分类名称的销售量。但是看起来我编写的代码有问题。请指导我如何在三个文件上执行聚合功能。
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>
下面是我的代码,但似乎无法正常工作。我知道这个工作的正确xquery是什么?
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>
答案 0 :(得分:0)
此查询:
for $total-sale in
let $vClasses := doc('file:///c:/temp/delete/classDescription.xml'),
$vDetails := doc('file:///c:/temp/delete/toyClassification.xml'),
$vSales := doc('file:///c:/temp/delete/toySale.xml')
return
for $c in $vClasses/*/*
return
let $d := $vDetails/*/*[classification eq $c/@name],
$s := $vSales/*/*[@toyID = $d/@toyID]
return
<classification name="{$c/@name}">
<totalSale>{sum($s/amount)}</totalSale>
</classification>
order by $total-sale/totalSale descending
return $total-sale
当三个XML文档驻留在相应的文件中:
C:/temp/delete/classDescription.xml:
<t>
<classification name="Electronic">
<Description>electronic devices that requires electric</Description>
</classification>
<classification name="SoftToy">
<Description>Fluffy toys that kids like</Description>
</classification>
</t>
C:/temp/delete/toyClassification.xml:
<t>
<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>
</t>
C:/temp/delete/toySale.xml:
<t>
<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>
</t>
会产生想要的正确结果:
<classification name="SoftToy">
<totalSale>27</totalSale>
</classification>
<classification name="Electronic">
<totalSale>10</totalSale>
</classification>
请注意,在其他两个表中根本不使用CompanyID数据。