Thufir
Thufir

Reputation: 8497

How to transform nested XML to CSV?

While not strictly XML at this point, looking to transform a "list" into something which would work for XML shredding, so something more like a table. Suitable for import to a spreadsheet or a table in a SQL database.

How would this input:

get transformed to a structure like:

joe phone1 phone2 phone3
sue cell4 home5
alice atrib6 x7 y9 z10 

It only matters that the "names" are in the first column, other "attributes" in any of following columns of CSV or similar, or something exportable as such CSV. It doesn't have to be CSV, perhaps just convertible to such a structure because BaseX exports to CSV quite nicely through the GUI.

or, perhaps:

joe phone1 
joe phone2 
joe phone3
sue cell4 
sue home5  
alice atrib6 
alice x7 
alice y9 
alice z10 

Although I prefer the former for this specific data.

Upvotes: 1

Views: 209

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

I think in another question you already got the suggestion to use windowing:

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method 'text';
declare option output:item-separator '
';

for tumbling window $group in ul/*
start $s next $n when $s[self::li] and $n[self::ul]
return
    tail($group) ! ($s || ',' || .)

https://xqueryfiddle.liberty-development.net/6qVSgeS

Upvotes: 4

Related Questions