如何在XQuery中添加少量节点的值后得到不同的值?

时间:2013-04-21 15:55:06

标签: xml xquery scraper

在xml以下:

<mo>
    <customers>
    <customer cno="2222">
            <cname>Charles</cname>
            <street>123 Main St.</street>
            <city>Wichita</city>
            <zip>67226</zip>
            <phone>316-636-5555</phone>
        </customer>
        <customer cno="1000">
            <cname>Bismita</cname>
            <street>Ashford Dunwoody</street>
            <city>Wichita</city>
            <zip>67226-1555</zip>
            <phone>000-000-0000</phone>
        </customer>     
    </customers>

    <employees>
    <employee eno="1000">
            <ename>Jones</ename>
            <city>Wichita</city>
            <zip>67226-1555</zip>
            <hdate>1995-12-12</hdate>
        </employee>
        <employee eno="2000">
            <ename>Asmit</ename>
            <city>Wichita</city>
            <zip>67226-1555</zip>
            <hdate>1967-08-13</hdate>
        </employee>
        <employee eno="1003">
            <ename>Axaya</ename>
            <city>BBSR</city>
            <zip>67226</zip>
            <hdate>1978-08-13</hdate>
        </employee>     
    </employees>


    <parts>
        <part pno="10506">          
            <pname>Land Before Time I</pname>
            <qoh>200</qoh>
            <price>319.99</price>
            <level>20</level>
        </part>
        <part pno="10000">          
            <pname>Bottle</pname>
            <qoh>2</qoh>
            <price>3.00</price>
            <level>2</level>
        </part>
        <part pno="10508">          
            <pname>Land Before Time 3</pname>
            <qoh>202</qoh>
            <price>2.00</price>
            <level>22</level>
        </part>
        <part pno="10509">          
            <pname>Cycle</pname>
            <qoh>202</qoh>
            <price>1.00</price>
            <level>22</level>
        </part>
    </parts>

    <orders>    
        <order ono="1000" takenBy="1000" customer="2222" >
            <receivedDate>1967-08-17</receivedDate>
            <shippedDate>1967-08-13</shippedDate>
            <items>
                <item>
                    <partNumber>10508</partNumber>
                    <quantity>2</quantity>
                </item>
            </items>
        </order>
        <order ono="1001" takenBy="1000" customer="1000" >
            <receivedDate>1968-08-14</receivedDate>
            <shippedDate>1968-08-11</shippedDate>
            <items>
                <item>
                    <partNumber>10000</partNumber>
                    <quantity>2</quantity>
                </item>

            </items>
        </order>

        <order ono="1022" takenBy="1003"  customer="2222" >
            <receivedDate>1995-02-14</receivedDate>
            <shippedDate>1995-02-13</shippedDate>
            <items>
                <item>
                    <partNumber>10000</partNumber>
                    <quantity>1</quantity>
                </item>
                <item>
                    <partNumber>10508</partNumber>
                    <quantity>2</quantity>
                </item>
                <item>
                    <partNumber>10509</partNumber>
                    <quantity>3</quantity>
                </item>
            </items>
        </order>

    </orders>
</mo>

如何获取每个订单的订单号及其总价? 我试过下面的xquery:

for
$o in /mo/orders/order,
$p in /mo/parts/part
where
$o/items/item/partNumber = data($p/@pno) 
return concat(data($o/@ono),'-', sum($p/price))

它返回订单号,总价格由' - '符号分隔,但由于订单中的多个商品而返回重复。 目前的结果:

1000-2 1001-3 1022-3 1022-2 1022-1

预期结果:

1000-2 1001-3 1022-6

任何帮助获得订单号与总价格的明确值???

1 个答案:

答案 0 :(得分:2)

使用我在您的其他问题中提出的价格计算并为每个订单运行它。问题在两个问题中完全相同,将fn:sum()置于错误的位置。

for $order in /mo/orders/order
return concat
  (
    $order/@ono,
    '-',
    fn:sum(
      for $item in $order/items/item
      return /mo/parts/part[@pno = $item/partNumber]/price * $item/quantity
    )
  )