3

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.

Joe Wicentowski
  • 5,159
  • 16
  • 26
Ranjith Reddy
  • 129
  • 1
  • 3
  • 12

1 Answers1

6

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

zeppelin
  • 8,947
  • 2
  • 24
  • 30