user1311286
user1311286

Reputation:

XQuery simple first time questions

I am new to XQuery and I just wanted to check and make sure that I have done there first few problems correctly and see if there is any feedback.

Find printer elements with price less than 100.

let $products := doc("products.xml")
for $e in $products/Products/Maker/Printer
where $e/Price < 100
return $e

Do the same thing but now produce the sequence of these elements surrounded by a tag.

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>

Upvotes: 2

Views: 354

Answers (1)

Jens Erat
Jens Erat

Reputation: 38712

Filtering results

Find printer elements with price less than 100.

There are no printers cheaper than 100 bucks. I used 200 for my examples. :)

You are querying an element, not the attribute. Add an @ before the attribute name. Also, XQuery is case sensitive, you will have to use @price:

let $products := doc("products.xml")
for $e in $products/Products/Maker/Printer
where $e/@price < 200
return $e

You can also try the much shorter XPath equivalent (which is a subset of XQuery):

doc("products.xml")/Products/Maker/Printer[@price<200]

Element construction

Do the same thing but now produce the sequence of these elements surrounded by a tag.

Now you will have to use "real" XQuery. You example is mostly fine (apart from the error fixed above), but currently you will return "$e" for each line. There are two ways to construct elements, also have a look at this handy tutorial:

  1. Curly brackets

    return <CheapPrinters>{$e}</CheapPrinters>

  2. Element Constructors

    return element {"CheapPrinters"} {$e}

Upvotes: 1

Related Questions