Home >>XQuery Tutorial >XQuery Sequences

XQuery Sequences

XQuery Sequences

Sequences represent an organized collection of items where things may be of various styles or equivalent.

Creating a sequence

The XQuery series is generated using quotes or double quotes or numbers in parentheses with strings inside. XML elements may be used as series objects too.

There are two methods to construct series items which are iterated one by one.

  • Using index
  • Using values

XQuery Expression for Index


let $items := ('orange', <apple/>, <fruit type="juicy"/>, <vehicle type="car">sentro</vehicle>, 1,2,3,'a','b',"abc")
let $count := count($items)
return
<result>
   <count>{$count}</count>
   
   <items>
      {
	     for $item in $items
         return <item>{$item}</item>
      }
   </items>
   
</result>

Output:
<result>
   <count>10</count>
   <items>
      <item>orange</item>
      <item>
         <apple/>
      </item>
      <item>
         <fruit type="juicy"/>
      </item>
      <item>
         <vehicle type="car">Sentro</vehicle>
      </item>
      <item>1</item>
      <item>2</item>
      <item>3</item>
      <item>a</item>
      <item>b</item>
      <item>abc</item>
   </items>
</result>

Viewing the Items of a Sequence

Sequence items can be iterated one by one, using index or by meaning. The illustration above iterated the series items, one by one. Let's put into practice the other two ways.

XQuery Expression (Index)


let $items := (1,2,3,4,5,6,7,8)
let $count := count($items)
return
   <result>
      <count>{$count}</count>
      
      <items>
      {
         for $item in $items[2]
         return <item>{$item}</item>
      }
      </items>
      
   </result>

Output:
<result>
   <count>8</count>
   <items>
      <item>2</item>
   </items>
</result>

XQuery Expression (Value)


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

Output:
<result>
   <count>8</count>
   <items>
      <item>1</item>
      <item>2</item>
      <item>3</item>
<item>4</item>
<item>5</item>
   </items>
</result>

How to run

To read the items.xqy, build a Java-based XQuery executor application, transfer it to the XQuery Expression Processor, and execute the expression. After that it shows the output.

XQueryTester.java


import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.InputStream;  
import javax.xml.xquery.XQConnection;  
import javax.xml.xquery.XQDataSource;  
import javax.xml.xquery.XQException;  
import javax.xml.xquery.XQPreparedExpression;  
import javax.xml.xquery.XQResultSequence;  
import com.saxonica.xqj.SaxonXQDataSource;  
public class XQueryTester {  
   public static void main(String[] args){  
      try {  
         execute();  
      }  
      catch (FileNotFoundException e) {  
         e.printStackTrace();  
      }        
      catch (XQException e) {  
         e.printStackTrace();  
      }  
   }  
   private static void execute() throws FileNotFoundException, XQException{  
      InputStream inputStream = new FileInputStream(new File("items.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));  
      }  
   }      
}  

Execute XQuery against XML

Put the three files above to the same location. We put them in a folder called XQuery7 on your desktop. Use terminal compile XQueryTester.java. You require JDK 1.5 or later to be enabled on your computer, and optimized classpaths.

Compile:

javac XQueryTester.java

Execute:

java XQueryTester

No Sidebar ads