Implementing Conditional Logic in XQuery with `if-then-else` Statements

Learn how to use XQuery's `if-then-else` statement to create conditional logic within your XML queries. This tutorial explains its syntax, demonstrates its use in various scenarios, and provides practical examples for building flexible and dynamic XQuery expressions that adapt to different data conditions.



Using XQuery `if-then-else` Statements

Understanding XQuery `if-then-else`

XQuery's `if-then-else` statement allows you to execute different code blocks based on a condition. This is a fundamental control flow mechanism used to create more flexible and dynamic XQuery expressions. It provides a way to create conditional logic within your XQuery code.

`if-then-else` Syntax

The syntax is:

if (condition) then expression1 else expression2

Where:

  • condition: An expression that evaluates to true or false.
  • expression1: The expression to evaluate if the condition is true.
  • expression2: The expression to evaluate if the condition is false.

Example: Conditional Book Title Retrieval

This example uses an `if-then-else` statement to check if the XML file exists before processing it. The XQuery expression retrieves book titles only if the price is above 30. The Java code executes the XQuery expression and handles potential exceptions.

1. Sample XML Data (`books.xml`)

books.xml

<books>
  <book price="400.00">
    <title>Learn Java Programming</title>
  </book>
  <book price="300.50">
    <title>DOTNET Fun</title>
  </book>
  <book price="250.00">
    <title>Learn XQuery</title>
  </book>
  <book price="150.50">
    <title>Learn XPath</title>
  </book>
</books>

2. XQuery Code (`books.xqy`)

books.xqy

{
  if(not(doc("books.xml"))) then ("books.xml does not exist")
  else (
    for $x in doc("books.xml")/books/book
    where $x/price > 30
    return $x/title
  )
}

3. Java Code (`XQueryTester.java`)

This Java code executes the XQuery. You need to have the Saxon library included in your project for this code to compile and run correctly.

Java Code

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.xml.xquery.*;
import com.saxonica.xqj.SaxonXQDataSource;

public class XQueryTester {
    public static void main(String[] args) {
        try {
            execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // ... execute() method ...
}
`execute()` Method

private static void execute() throws Exception {
    InputStream inputStream = new FileInputStream(new File("books.xqy"));
    XQDataSource ds = new SaxonXQDataSource();
    XQConnection conn = ds.getConnection();
    XQPreparedExpression exp = conn.prepareExpression(inputStream);
    XQResultSequence result = exp.executeQuery();
    while (result.next()) {
        System.out.println(result.getItemAsString(null));
    }
}

Conclusion

The XQuery `if-then-else` statement is a basic but important tool for adding conditional logic to your XQuery expressions, enabling you to create flexible queries that adapt to different situations and data.