2012-10-07 159 views
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> 

回答

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數據ISN」在其他兩個表中完全可以使用。

+0

嗨dimitre,已經試過你的查詢,是的,它運作良好。再次學習新的東西。謝謝 – setiasetia