Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert XML data to CSV String with XQuery

Tags:

xml

xquery

osb

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.

like image 954
Ranjith Reddy Avatar asked Apr 13 '26 23:04

Ranjith Reddy


1 Answers

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

like image 131
zeppelin Avatar answered Apr 16 '26 16:04

zeppelin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!