0

Consider an xml file defined as:

<?xml version="1.0"?>
<sports>
    <teams>
    <team tno="t100">
            <tname>Knights</tname>
            <city>London</city>
        </team>
        <team tno="t200">
            <tname>Dukes</tname>
            <city>Surrey</city>
        </team> 
        <team tno="t300">
            <tname>Kings</tname>
            <city>Leeds</city>
        </team>     
    </teams>

    <players>
    <player pid="p501">
            <pname>Simon</pname>
            <city>London</city>
        </player>
        <player pid="p502">
            <pname>Andrew</pname>
            <city>Birmingham</city>
        </player>
        <player pid="p503">
            <pname>Mike</pname>
            <city>London</city>
        </player>       
    </players>


    <parts>
        <part pno="801">            
            <pname>Right Gloves</pname>
            <price>8.99</price>
        </part>
        <part pno="901">            
            <pname>Left Gloves</pname>
            <price>9.99</price>
        </part>
        <part pno="851">            
            <pname>Left Pad</pname>
            <price>33.00</price>
        </part>
        <part pno="951">            
            <pname>Right Pad</pname>
            <price>43.00</price>
        </part>
    </parts>

    <orders>    
        <order ono="61" playerNum="p501" team="t200" >
            <kits>
                <kit>
                    <pNum>801</pNum>
                    <qty>11</qty>
                </kit>
            </kits>
        </order>
        <order ono="62" playerNum="p501" team="t100" >
            <kits>
                <kit>
                    <pNum>901</pNum>
                    <qty>12</qty>
                </kit>

            </kits>
        </order>

        <order ono="63" playerNum="p502" team="t300" >
            <kits>
                <kit>
                    <pNum>851</pNum>
                    <qty>9</qty>
                </kit>

            </kits>
        </order>

        <order ono="64" playerNum="p503" team="t300" >
            <kits>
                <kit>
                    <pNum>951</pNum>
                    <qty>16</qty>
                </kit>

            </kits>
        </order>


        <order ono="65" playerNum="p503"  team="t200" >
            <kits>
                <kit>
                    <pNum>801</pNum>
                    <qty>12</qty>
                </kit>
                <kit>
                    <pNum>901</pNum>
                    <qty>16</qty>
                </kit>
                <kit>
                    <pNum>851</pNum>
                    <qty>13</qty>
                </kit>
            </kits>
        </order>

    </orders>
</sports>

The query that I am trying to run is to get total pricing for a certain order (say order 65). The query in BaseX is:

let $d:=doc("sports.xml")
let $o:=$d/sports/orders/order[@ono=65]
let $p:=$d/sports/parts/part
for $k in $o/kits/kit 
return <OrderCost>
({$k/qty})*({$p[@pno=$k/pNum]/price})
</OrderCost>

My output:

<OrderCost>
(<qty>12</qty>)*(<price>8.99</price>)
</OrderCost>
<OrderCost>
(<qty>16</qty>)*(<price>9.99</price>)
</OrderCost>
<OrderCost>
(<qty>13</qty>)*(<price>33.00</price>)
</OrderCost>

I am unable to multiply the quantity with corresponding price and then get the sum. What am I doing wrong here?

dirkk
  • 6,160
  • 5
  • 33
  • 51
xs2dhillon
  • 23
  • 4

1 Answers1

2

When creating elements, everything inside curly brackets is evaluated (and nothing outside curly brackets is evaluated). As you've only put braces around your variables, only these get replaced, but the multiplication is not performed.

Replace by this line:

{ $k/qty * $p[@pno=$k/pNum]/price }
C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
Jens Erat
  • 37,523
  • 16
  • 80
  • 96
  • Thanks Jens, that worked. However, is there any such constraint on the 'sum()' function as well. When I modify my code to get a sum of all prices, instead of the sum of all item, 'sum' gets printed. – xs2dhillon Apr 24 '13 at 12:41
  • Here is modified code: ** let $d:=doc("sports.xml") let $o:=$d/sports/orders/order[@ono=65] let $p:=$d/sports/parts/part for $k in $o/kits/kit return sum({ $k/qty * $p[@pno=$k/pNum]/price }) ** and below is the o/p: ** sum(107.88) sum(159.84) sum(429) ** – xs2dhillon Apr 24 '13 at 12:42
  • As you want `sum()` to get evaluated, it must be within the curly brackets, too. – Jens Erat Apr 24 '13 at 12:53