Home >>XQuery Tutorial >XQuery string join() Function

XQuery string join() Function

XQuery string join() Function

The method XQuery string-join is used for the concatenation of various sequences separated by a specified 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 illustration to illustrate use of the feature XQuery string-join. Taking an XQuery expression named "courses.xqy," which has the code below. It will concat all courses offered from the provided expression XQuery.

XQuery Expression: courses.xqy:


let $courses :=  
<courses>  
   <course>PHP</course>  
   <course>JAVA</course>  
   <course>C/C++</course>  
   <course>Oracle</course>  
</courses>  
return  
<results>     
   <courses>{  
      string-join($courses/course, ',')  
   }</courses>  
</results>  

To read the courses.xqy, build a Java-based XQuery executor system, 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("courses.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 two files above to the same location. We put them in a folder called XQuery15 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