XQuery Sequences

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

XQuery Sequences

XQuery sequences are used to specify an ordered collection of items. These items can be of similar or different types.


Creating a sequence

An XQuery sequence is created using parenthesis with strings inside quotes or double quotes or numbers. XML elements can also be used as the items of a sequence.

There are two ways to create items of sequence iterated one by one.

  • Using index
  • Using values

XQuery Expression for Index

items.xqy

  1. let $items := ('orange', <apple/><fruit type="juicy"/><vehicle type="car">sentro</vehicle>, 1,2,3,'a','b',"abc")  
  2. let $count :count($items)  
  3. return  
  4. <result>  
  5.    <count>{$count}</count>     
  6.    <items>  
  7.       {  
  8.          for $item in $items  
  9.          return <item>{$item}</item>  
  10.       }  
  11.    </items>     
  12. </result>  

How to run

Create a Java based XQuery executor program to read the items.xqy, passes it to the XQuery expression processor, and executes the expression. After that the result will be displayed.

XQueryTester.java

  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.InputStream;  
  5. import javax.xml.xquery.XQConnection;  
  6. import javax.xml.xquery.XQDataSource;  
  7. import javax.xml.xquery.XQException;  
  8. import javax.xml.xquery.XQPreparedExpression;  
  9. import javax.xml.xquery.XQResultSequence;  
  10. import com.saxonica.xqj.SaxonXQDataSource;  
  11. public class XQueryTester {  
  12.    public static void main(String[] args){  
  13.       try {  
  14.          execute();  
  15.       }  
  16.       catch (FileNotFoundException e) {  
  17.          e.printStackTrace();  
  18.       }        
  19.       catch (XQException e) {  
  20.          e.printStackTrace();  
  21.       }  
  22.    }  
  23.    private static void execute() throws FileNotFoundException, XQException{  
  24.       InputStream inputStream = new FileInputStream(new File("items.xqy"));  
  25.       XQDataSource ds = new SaxonXQDataSource();  
  26.       XQConnection conn = ds.getConnection();  
  27.       XQPreparedExpression exp = conn.prepareExpression(inputStream);  
  28.       XQResultSequence result = exp.executeQuery();  
  29.        while (result.next()) {  
  30.          System.out.println(result.getItemAsString(null));  
  31.       }  
  32.    }      
  33. }  

Execute XQuery against XML

Put the above three files to a same location. We put them on desktop in a folder name XQuery7. 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 Sequence 1


XQuery Expression for Index

items.xqy

  1. let $items := (1,2,3,4,5,6)  
  2. let $count :count($items)  
  3. return  
  4.    <result>  
  5.       <count>{$count}</count>    
  6.       <items>  
  7.       {  
  8.          for $item in $items  
  9.          return <item>{$item}</item>  
  10.       }  
  11.       </items>     
  12.    </result>  

Follow the same procedure as above:

Output:

XQUERY Sequence 2