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.
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:
let $courses := <courses> <course>Java</course> <course>DotNet</course> <course>C/C++</course> <course>Oracle</course> </courses> return <results> <courses>{ string-join($courses/course, ',') }</courses> </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
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)); } } }
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: