XQuery `string-join()` Function: Concatenating Strings with a Specified Delimiter
Learn how to use XQuery's `string-join()` function to efficiently concatenate sequences of strings. This tutorial explains its syntax, demonstrates its application with various delimiters, and provides practical examples for formatting and combining string data within XML documents.
Using XQuery's `string-join()` Function
The XQuery `string-join()` function concatenates (joins) a sequence of strings into a single string, using a specified delimiter. This is a very useful function for formatting or combining string data within XML documents.
`string-join()` Function Syntax
string-join($sequence as xs:string*, $delimiter as xs:string) as xs:string
The function takes two arguments:
$sequence
: A sequence of strings to join.$delimiter
: The string to insert between each element of the sequence.
The function returns a single string containing all the input strings, separated by the delimiter.
Example: Joining Course Names
This example shows how to use `string-join()` to combine course names. (Note: This example requires an XQuery processor and the Saxon HE library. Screenshots from the original text are not included here. Please refer to the original document for visual verification of the example. The descriptions below aim to convey the information in those screenshots.)
`courses.xqy` (XQuery File)
let $courses := ("Java", "DotNet", "C/C++", "Oracle")
return string-join($courses, ',')
`XQueryTester.java` (Java Program)
import java.io.File;
import java.io.FileInputStream;
// ... (other imports) ...
public class XQueryTester {
public static void main(String[] args) {
// ... (code to execute the XQuery expression) ...
}
}
This Java program uses the Saxon library to execute the XQuery expression.