XML to CSV Conversion with Xquery

88 Views Asked by At

I need to convert below XML to CSV such that for null xml tag there must not be any comma in CSV file.

for example , consider below XML

<data>
<user>
<id>1</id>
<name>jaison</name>
<age>21</age>
</user>
<user>
<id>2</id>
<name>Monson</name>
<age></age>
</user>
</data>

expected csv:
id,name,age
1,jaison,21
2,Monson
1

There are 1 best solutions below

0
Martin Honnen On

One way would be to try to add a [normalize-space()] predicate to all element selections e.g.

for $b in $req//ns1:topLevelArray     
return            
  concat(escape-html-uri(
   string-join(($b/ns1:ID[normalize-space()],                                           
                $b/ns1:name[normalize-space()],                                           
                $b/ns1:PersonID[normalize-space()],                                           
                $b/ns1:age[normalize-space()]                                                              
               )/normalize-space(),                                         
               ",") 
    ), codepoints-to-string(10)) }