Below are several XQuery and XPath examples ranging from beginner to advanced, using your
books.xml
file.
Beginner XPath Examples:
-
Select all book titles: XPath Query:
/catalog/book/title -
Select all authors: XPath Query: /catalog/book/author
-
Count the number of books: XPath Query: count(/catalog/book)
-
Select books in the "Computer" genre: XPath Query: /catalog/book[genre = 'Computer']
Intermediate XPath Examples:
-
Select books with a price higher than 20: XPath Query: /catalog/book[price > 20]
-
Select the title and publish date for books published after 2000-12-05: XPath Query: /catalog/book[publish_date > '2000-12-05']/(title, publish_date)
-
Select books with titles containing the text "Guide": XPath Query: /catalog/book[contains(title, 'Guide')]
Beginner XQuery Examples:
-
Return all book titles using XQuery: XQuery: for $b in doc("books.xml")//book return $b/title
-
Return title and author for each book: XQuery: for $b in doc("books.xml")//book return <title>{ $b/title/text() }</title> { $b/author/text() }
Intermediate XQuery Examples:
-
Filter books with price less than or equal to 10 and return as XML: XQuery: for $b in doc("books.xml")//book where xs:decimal($b/price) <= 10 return { $b/title } { $b/author }
-
Return books sorted by publish date (ascending): XQuery: for $b in doc("books.xml")//book order by xs:date($b/publish_date) return $b
Advanced XQuery Examples:
-
Group books by genre and count the number in each group: XQuery (using FLWOR and grouping): for $g in distinct-values(doc("books.xml")//book/genre) let $books := doc("books.xml")//book[genre = $g] return { count($books) } { $books }
-
Return books with detailed info and an extra element showing if the price is considered "Low" (<15), "Medium" (15-30) or "High" (>30): XQuery: for $b in doc("books.xml")//book let $price := xs:decimal($b/price) let $range := if ($price < 15) then 'Low' else if ($price <= 30) then 'Medium' else 'High' return <title>{ $b/title }</title> { $b/author } { $b/price } <publish_date>{ $b/publish_date }</publish_date>
-
Advanced XPath: Using union to return all titles and genres in one result set: XPath Query: /catalog/book/title | /catalog/book/genre
Notes:
• Replace doc("books.xml") with the appropriate file path if needed. • These examples assume your XML root is and each element contains common subelements. • Run these queries using an XQuery processor or an XML-enabled IDE (such as VS Code with an XML/XQuery extension).
These examples provide a progressive tutorial from simple XPath selections to advanced XQuery grouping and conditional logic.