XQuery查询计数

时间:2012-08-02 23:21:17

标签: xml xquery

我第一次学习XQuery,而且我遇到了一个特定查询问题。

找到生产至少两台PC的制造商名称,速度至少为2.00。

我已经得到了速度< = 2.00部分的查询,但我无法弄清楚如何按发生次数对其进行分组。

尝试:(什么都不输出)

let $doc := doc("PC.xml")
for $x in $doc/Products/Maker/PC
where $x/Speed>=2.00 and count($x)>=2
return $x/../@name

PC.xml:

<Products>
    <Maker name = "A">
        <PC model = "1001" price = "2114">
            <Speed>2.66</Speed>
            <RAM>1024</RAM>
            <HardDisk>250</HardDisk>
        </PC>
        <PC model = "1002" price = "995">
            <Speed>2.10</Speed>
            <RAM>512</RAM>
            <HardDisk>250</HardDisk>
        </PC>
        <Laptop model = "2004" price = "1150">
            <Speed>2.00</Speed>
            <RAM>512</RAM>
            <HardDisk>60</HardDisk>
            <Screen>13.3</Screen>
        </Laptop>
        <Laptop model = "2005" price = "2500">
            <Speed>2.16</Speed>
            <RAM>1024</RAM>
            <HardDisk>120</HardDisk>
            <Screen>17.0</Screen>
        </Laptop>
    </Maker>
    <Maker name = "E">
        <PC model = "1011" price = "959">
            <Speed>1.86</Speed>
            <RAM>2048</RAM>
            <HardDisk>160</HardDisk>
        </PC>
        <PC model = "1012" price = "649">
            <Speed>2.80</Speed>
            <RAM>1024</RAM>
            <HardDisk>160</HardDisk>
        </PC>
        <Laptop model = "2001" price = "3673">
            <Speed>2.00</Speed>
            <RAM>2048</RAM>
            <HardDisk>240</HardDisk>
            <Screen>20.1</Screen>
        </Laptop>
        <Printer model = "3002" price = "239">
            <Color>false</Color>
            <Type>laser</Type>
        </Printer>
    </Maker>
    <Maker name = "H">
        <Printer model = "3006" price = "100">
            <Color>true</Color>
            <Type>ink-­-jet</Type>
        </Printer>
        <Printer model = "3007" price = "200">
            <Color>true</Color>
            <Type>laser</Type>
        </Printer>
    </Maker>
</Products>

2 个答案:

答案 0 :(得分:2)

egads,我已经弄清楚了!

let $doc := doc("PC.xml")/Products
for $maker in $doc//Maker
where count($maker/PC[Speed >= 3.00]) >= 2
return data($maker/@name)

答案 1 :(得分:0)

使用这个更简单的表达式:

/*/Maker[PC[number(Speed) ge 2.00][2]]/@name/string()