Question

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?

Was it helpful?

Solution

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 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top