2

我是第一次学习 XQuery,但在使用特定查询时遇到了麻烦。

找出生产至少两台速度至少为 2.00 的 PC 的 Maker 的名字。

我已经得到了查询的 Speed<=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>
4

2 回答 2

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)
于 2012-08-03T01:13:06.837 回答
0

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

/*/Maker[PC[number(Speed) ge 2.00][2]]/@name/string()
于 2012-08-03T04:44:29.547 回答