XQuery If Then Else Statement

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

XQuery If Then Else Statement

The XQuery If Then Else statement is used to check the validity of the passing input values.

  1. if (condition) then  
  2.  ...   
  3. else  
  4.  ...   

XQuery If Then Else Example

Let's take an example to demonstrate the usage of if-then-else statement in XQuery. Take an XML file named books.xml and apply to it XQuery expression containing an if-then-else construct to retrieve the titles of those books where price is greater than 100.

XML statementM

books.xml

  1. xml version="1.0" encoding="UTF-8"?>  
  2. <books>  
  3.    <book category="JAVA">  
  4.       <title lang="en">Learn Java Programmingtitle>  
  5.       <author>Sonoo Jaiswalauthor>  
  6.       <year>2012year>  
  7.       <price>400.00price>  
  8.    book>  
  9.      
  10.    <book category="DOTNET">  
  11.       <title lang="en">DOTNET Funtitle>  
  12.       <author>Balaswamyauthor>  
  13.       <year>2008year>  
  14.       <price>300.50price>  
  15.    book>  
  16.      
  17.    <book category="XML">  
  18.       <title lang="en">Learn XQuery in 1 monthtitle>  
  19.       <author>Ajeet Kumarauthor>  
  20.       <author>Sam Leeauthor>  
  21.       <year>2013year>  
  22.       <price>250.00price>  
  23.    book>  
  24.      
  25.    <book category="XML">  
  26.       <title lang="en">Learn XPath in 1 monthtitle>  
  27.       <author>Ajeet Kumarauthor>  
  28.       <year>2014year>  
  29.       <price>150.50price>  
  30.    book>  
  31. books>  

 

Xquery expression

books.xqy:

  1. <result>  
  2. {  
  3.    if(not(doc("books.xml"))) then (  
  4.       <error>  
  5.          <message>books.xml does not existmessage>  
  6.       error>  
  7.    )  
  8.    else (   
  9.       for $x in doc("books.xml")/books/book   
  10.       where $x/price>30  
  11.       return $x/title  
  12.    )  
  13. }  
  14. result>  
  15.   
  16. Create a Java based XQuery executor program to read the books.xqy,  
  17.  passes it to the XQuery expression processor, and executes the expression. After that the result will be displayed.   
  18. XQueryTester.java  
  19.   
  20. import java.io.File;  
  21. import java.io.FileInputStream;  
  22. import java.io.FileNotFoundException;  
  23. import java.io.InputStream;  
  24.   
  25. import javax.xml.xquery.XQConnection;  
  26. import javax.xml.xquery.XQDataSource;  
  27. import javax.xml.xquery.XQException;  
  28. import javax.xml.xquery.XQPreparedExpression;  
  29. import javax.xml.xquery.XQResultSequence;  
  30.   
  31. import com.saxonica.xqj.SaxonXQDataSource;  
  32.   
  33. public class XQueryTester {  
  34.    public static void main(String[] args){  
  35.       try {  
  36.          execute();  
  37.       }  
  38.         
  39.       catch (FileNotFoundException e) {  
  40.          e.printStackTrace();  
  41.       }  
  42.         
  43.       catch (XQException e) {  
  44.          e.printStackTrace();  
  45.       }  
  46.    }  
  47.   
  48.    private static void execute() throws FileNotFoundException, XQException{  
  49.       InputStream inputStream = new FileInputStream(new File("books.xqy"));  
  50.       XQDataSource ds = new SaxonXQDataSource();  
  51.       XQConnection conn = ds.getConnection();  
  52.       XQPreparedExpression exp = conn.prepareExpression(inputStream);  
  53.       XQResultSequence result = exp.executeQuery();  
  54.        while (result.next()) {  
  55.          System.out.println(result.getItemAsString(null));  
  56.       }  
  57.    }      
  58. }  

Execute XQuery against XML

Put the above three files to a same location. We put them on desktop in a folder name XQuery9. Compile XQueryTester.java using console. You must have JDK 1.5 or later installed on your computer and classpaths are configured.

Compile:

javac XQueryTester.java

Execute:

java XQueryTester

Output:

XQUERY If then else statement 1