XQuery String Join Function

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

XQuery string-join() Function

The XQuery string-join function is used to concatenate various sequences separated by a given delimiter.

string-join($sequence as xs:string*, $delimiter as xs:string) as xs:string 

Parameter explanation:

$sequence - It specifies the sequence of zero or more strings.

$delimiter - It specifies the delimiter to separate the items of above sequence.


XQuery string-join Example

Let's take an example to demonstrate the usage of XQuery string-join function. Take an XQuery expression named "courses.xqy", having the following code. It will concat all the given courses from the given XQuery expression.

XQuery Expression:

courses.xqy:

  1. let $courses :=  
  2. <courses>  
  3.    <course>Javacourse>  
  4.    <course>DotNetcourse>  
  5.    <course>C/C++course>  
  6.    <course>Oraclecourse>  
  7. courses>  
  8. return  
  9. <results>     
  10.    <courses>{  
  11.       string-join($courses/course, ',')  
  12.    }courses>  
  13. results>  

Create a Java based XQuery executor program to read the courses.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.   
  6. import javax.xml.xquery.XQConnection;  
  7. import javax.xml.xquery.XQDataSource;  
  8. import javax.xml.xquery.XQException;  
  9. import javax.xml.xquery.XQPreparedExpression;  
  10. import javax.xml.xquery.XQResultSequence;  
  11.   
  12. import com.saxonica.xqj.SaxonXQDataSource;  
  13.   
  14. public class XQueryTester {  
  15.    public static void main(String[] args){  
  16.       try {  
  17.          execute();  
  18.       }  
  19.         
  20.       catch (FileNotFoundException e) {  
  21.          e.printStackTrace();  
  22.       }  
  23.         
  24.       catch (XQException e) {  
  25.          e.printStackTrace();  
  26.       }  
  27.    }  
  28.   
  29.    private static void execute() throws FileNotFoundException, XQException{  
  30.       InputStream inputStream = new FileInputStream(new File("courses.xqy"));  
  31.       XQDataSource ds = new SaxonXQDataSource();  
  32.       XQConnection conn = ds.getConnection();  
  33.       XQPreparedExpression exp = conn.prepareExpression(inputStream);  
  34.       XQResultSequence result = exp.executeQuery();  
  35.        while (result.next()) {  
  36.          System.out.println(result.getItemAsString(null));  
  37.       }  
  38.    }      

Execute XQuery against XML

Put the above both files to a same location. We put them on desktop in a folder name XQuery15. 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 String join function 1