3
votes

Consider the following XML:

<LIST>
    <Name>Jon</Name>
    <Name>Dan</Name>
    <Name>Bill</Name>
    <Name>Jack</Name>
</LIST>

I need output as a string as a CSV like Jon,Dan,Bill,Jack using XQuery.

I have done it using FLWOR expressions and normalize-space and then replacing the spaces with commas. However, I believe there should be a better way to do it in XQuery.

1

1 Answers

6
votes

You can use string-join function to do this:

string-join(//Name/text(),",")

fn:string-join($arg1 as xs:string*, $arg2 as xs:string) as xs:string

Returns a xs:string created by concatenating the members of the $arg1 sequence using $arg2 as a separator.

https://www.w3.org/TR/xpath-functions/#func-string-join