XQuery Select

Selecting and extracting data from XML documents

🔍 What is XQuery Select?

XQuery Select operations retrieve specific data from XML documents using path expressions and predicates. You can filter, extract, and transform XML content precisely to get exactly the information you need.


(: Select book titles :)
for $book in //book
return $book/title
                                    

Selection Methods

XQuery offers powerful methods to select and filter XML data. These techniques help you navigate complex XML structures and extract relevant information efficiently.

📍

Path Selection

Navigate XML tree structure

//book/title
🎯

Predicate Filter

Filter with conditions

//book[@price < 30]
🔄

FLWOR Select

Complex selection logic

for $x in //book
where ...
📊

Attribute Select

Extract attribute values

//book/@id

🔹 Basic Selection

Select elements using path expressions:

<!-- Sample XML -->
<library>
  <book id="1">
    <title>XQuery Basics</title>
    <author>John Doe</author>
    <price>29.99</price>
  </book>
  <book id="2">
    <title>XML Guide</title>
    <author>Jane Smith</author>
    <price>39.99</price>
  </book>
</library>
(: Select all titles :)
//title

(: Select all books :)
//book

(: Select specific path :)
/library/book/title

Result:

<title>XQuery Basics</title>
<title>XML Guide</title>

🔹 Selecting with Predicates

Filter selections using conditions:

(: Select books under $30 :)
//book[@price < 30]

(: Select by position :)
//book[1]  (: First book :)
//book[last()]  (: Last book :)

(: Select by attribute :)
//book[@id="1"]

(: Multiple conditions :)
//book[@price < 35 and author="John Doe"]

Result for price < 30:

<book id="1">
  <title>XQuery Basics</title>
  <author>John Doe</author>
  <price>29.99</price>
</book>

🔹 Selecting Attributes

Extract attribute values from elements:

(: Select all id attributes :)
//book/@id

(: Select specific attribute :)
//book[@id="1"]/@price

(: Use in FLWOR :)
for $book in //book
return $book/@id

Result:

1
2

🔹 Selecting with FLWOR

Use FLWOR for complex selections:

(: Select and filter :)
for $book in //book
where $book/price < 35
return $book/title

(: Select with sorting :)
for $book in //book
order by $book/price
return 
  <item>
    {$book/title}
    {$book/price}
  </item>

(: Select with calculations :)
for $book in //book
let $discount := $book/price * 0.1
where $discount > 2
return $book/title

🔹 Selecting Text Content

Extract text values from elements:

(: Get text content :)
//book/title/text()

(: Get string value :)
string(//book[1]/title)

(: Get data value :)
data(//book/price)

(: Concatenate text :)
for $book in //book
return concat($book/title, " by ", $book/author)

Result:

XQuery Basics by John Doe
XML Guide by Jane Smith

🔹 Selecting Distinct Values

Get unique values from selections:

(: Select distinct authors :)
distinct-values(//book/author)

(: Select distinct categories :)
distinct-values(//book/@category)

(: Count distinct values :)
count(distinct-values(//book/author))

🔹 Conditional Selection

Select based on conditions:

(: Select with if-then-else :)
for $book in //book
return
  if ($book/price < 30)
  then <affordable>{$book/title}</affordable>
  else <expensive>{$book/title}</expensive>

(: Select elements that exist :)
//book[author]  (: Books with author element :)
//book[not(isbn)]  (: Books without isbn :)

🧠 Test Your Knowledge

What does //book[1] select?