XQuery (XML Query Language) is designed to query and manipulate XML data. It provides a powerful way to navigate, transform, and extract information from XML documents.
Key Features of XQuery
Data Retrieval: XQuery allows you to extract specific data from XML documents using a structured query syntax.
Data Manipulation: You can update, insert, or delete nodes in XML documents.
Integration with XPath: XQuery uses XPath expressions to navigate through XML trees.
Support for Functions: It includes built-in functions for string manipulation, date handling, and more.
Basic Syntax
An XQuery expression typically follows this structure:
xquery
for $variable in $sequence
let $anotherVariable := expression
where condition
order by $sortExpression
return expression
for $book in doc("library.xml")/library/book
return $book/title
2. Filter Books by Price
To find books priced over 30:
xquery
for $book in doc("library.xml")/library/book
where $book/price > 30
return <result><title>{$book/title/text()}</title><author>{$book/author/text()}</author></result>
3. Sort Books by Title
To sort books alphabetically by title:
xquery
for $book in doc("library.xml")/library/book
order by $book/title
return <result><title>{$book/title/text()}</title><author>{$book/author/text()}</author></result>
4. Create a New XML Structure
To create a new XML catalog from the existing data:
XQuery is a powerful language for querying and manipulating XML data. With its rich syntax and capabilities, it allows developers to efficiently extract and transform XML information. If you have specific requirements or need further examples, feel free to ask!