我是XQuery的新手,我只想检查并确保我已经在那里完成了几个问题,看看是否有任何反馈。
查找价格低于100的打印机元素。
let $products := doc("products.xml")
for $e in $products/Products/Maker/Printer
where $e/Price < 100
return $e
执行相同的操作,但现在生成由标记包围的这些元素的序列。
let $products := doc("products.xml")
for $e in $products/Products/Maker/Printer
where $e/Price < 100
return <CheapPrinters>$e</CheapPrinters>
Products.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>
答案 0 :(得分:1)
查找价格低于100的打印机元素。
没有比100美元便宜的打印机。我用了200个例子。 :)
您正在查询元素,而不是属性。在属性名称前添加@
。此外,XQuery区分大小写,您必须使用@price
:
let $products := doc("products.xml")
for $e in $products/Products/Maker/Printer
where $e/@price < 200
return $e
你也可以尝试更短的XPath等价物(它是XQuery的一个子集):
doc("products.xml")/Products/Maker/Printer[@price<200]
执行相同的操作,但现在生成由标记包围的这些元素的序列。
现在你将不得不使用“真正的”XQuery。你的例子大部分都很好(除了上面修正的错误),但是目前你会为每一行返回“$ e”。有两种方法可以构建元素,也可以查看这个handy tutorial:
卷曲括号
return <CheapPrinters>{$e}</CheapPrinters>
元素构造函数
return element {"CheapPrinters"} {$e}