2

我有如下 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>
4

1 回答 1

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 数据。

于 2012-10-07T15:37:58.380 回答