XQuery String-join()
In XQuery, the fn:string-join()
function is used to concatenate a sequence of strings into a single string using a specified separator. This function takes two arguments: the sequence of strings to be joined and the separator string. It returns a single string that is the result of concatenating the input strings with the separator in between each pair of adjacent strings.
Here’s the syntax of the fn:string-join()
function:
fn:string-join($sequence, $separator)
$sequence
: The sequence of strings to be joined together.$separator
: The separator string used between each pair of adjacent strings.
Here’s an example of using fn:string-join()
in XQuery:
let $fruits := ("apple", "banana", "orange", "grape")
let $result := fn:string-join($fruits, ", ")
return $result
Output:
"apple, banana, orange, grape"
In this example, we have a sequence of strings representing different fruits stored in the variable $fruits
. We use the fn:string-join($fruits, ", ")
function to join these strings together with a comma and space (“, “) as the separator, and the result is stored in the variable $result
.
If the input sequence is empty or contains only one item, the fn:string-join()
function will return that item without applying the separator.
Keep in mind that the fn:string-join()
function works with sequences of strings, and if you have a sequence of non-string values, you may need to convert them to strings before using this function. For example, you can use the string()
function to convert non-string values to strings if needed.
let $numbers := (1, 2, 3, 4, 5)
let $result := fn:string-join($numbers ! string(), ", ")
return $result
Output:
"1, 2, 3, 4, 5"
In this example, we used the !
operator (sequence constructor) to apply the string()
function to each item in the $numbers
sequence before joining them with the separator.