</>
Skip to content
XML lessons (13/29)

XML — XQuery

What is XQuery?

XQuery is a query language designed for XML data, similar to SQL for databases.

FLWOR Expression

for $book in doc("books.xml")//book
where $book/price > 30
order by $book/title
return $book/title

FLWOR Components

KeywordDescription
forIterate over nodes
whereFilter conditions
letAssign variables
order bySort results
returnOutput expression

Basic Queries

// Return all book titles
doc("books.xml")//book/title

// Return books with price > 30
doc("books.xml")//book[price > 30]

// Count books
count(doc("books.xml")//book)

String Functions

contains(//title, "XML")
starts-with(//title, "Learning")
substring(//title, 1, 5)

Constructing XML

<results>
{
    for $book in doc("books.xml")//book
    return <book>
        <title>{$book/title/text()}</title>
    </book>
}
</results>

XQuery vs XPath

FeatureXQueryXPath
ComplexityFull languageExpression only
LogicConditional, loopsLimited
ConstructionCan build XMLCan't build XML

Mini Practice

  1. Write a FLWOR expression
  2. Filter and sort results
  3. Use string functions
  4. Construct XML output

Up Next

Continue with XSLT — transforming XML documents.

Related Topics

Frequently Asked Questions about XQuery

What is XQuery in XML?

XQuery is a fundamental concept in XML. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn XQuery?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn XQuery.

Why is XQuery important in XML?

XQuery is essential for XML development. Understanding this concept will help you write better code and solve real-world problems more effectively.