質問

I'm using XQUERY to perform addition: following is the structure of XML saved in database:

<?xml version="1.0" encoding="UTF-8"?>
<shop>
<products>
    <product cod="P0002">
        <name>Windows 8</name>
        <price unit="Euros">120</price>
    </product>
    <product cod="P0008">
        <name>Pendrive 32GB</name>
        <price unit="Euros">16</price>
    </product>
    <product cod="P0004">
        <name>Laptop ASUS</name>
        <price unit="Euros">400</price>
    </product>
    <product cod="P1600">
        <name>Laptop Mouse</name>
        <price unit="Euros">10</price>
    </product>
    <product cod="P3000">
        <name>Microsoft Office 2013</name>
        <price unit="Euros">119</price>
    </product>        
    <product cod="P5000">
        <name>Hard Disk 1 TB</name>
        <price unit="Euros">78</price>
    </product>
</products>   
<shoppings>
    <amount product="P0004">5</amount>
    <amount product="P0002">3</amount>
    <amount product="P0004">7</amount>
    <amount product="P1600">2</amount>
    <amount product="P0004">1</amount>
    <amount product="P0008">3</amount>
</shoppings>

I need to display the name of each product and the total number of units sold (or 0 if it has not sold any). I need this result but im trying and trying and no success:

<product>
   <name>Windows 8</name>
   <amount>3</amount>
</product>
<product>
   <name>Pendrive 32GB</name>
   <amount>3</amount>
</product>
<product>
   <name>Laptop ASUS</name>
   <amount>13</amount>
</product>
<product>
   <name>Laptop Mouse</name>
   <amount>2</amount>
</product>
<product>
   <name>Microsoft Office 2013</name>
   <amount>0</amount>
</product>
<product>
   <name>HArd Disk 1 TB</name>
   <amount>0</amount>
</product>
役に立ちましたか?

解決

for $p in //product return
<product ...>
  <amount>{sum(//amount[@product=$p/@cod])}</amount>
</product>

他のヒント

Be nice if you shared some of your attempts. This hint good enough?

let $amount := sum(distinct-values($node//shoppings/amount[@product ='P0004']))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top