Skip to content

Instantly share code, notes, and snippets.

@EdWarga
Created December 11, 2015 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdWarga/b5395a348a7b5874a4ef to your computer and use it in GitHub Desktop.
Save EdWarga/b5395a348a7b5874a4ef to your computer and use it in GitHub Desktop.
Convert CFRegister Data to CSV
xquery version "3.1";
declare namespace xpf = "http://www.w3.org/2005/xpath-functions";
(: Connects to the Comédie Française registers, converts JSON data to XML, and (optionally) persists documents to BaseX database :)
(:Based on https://gist.github.com/CliffordAnderson/d8ee47849964f75030e9:)
declare function local:convert-play($map as element()) as element()
{
element play {
attribute id {$map/xpf:number[@key="id"]/text()},
attribute created {$map/xpf:string[@key="created_at"]/text()},
attribute updated {$map/xpf:string[@key="updated_at"]/text()},
attribute validated {$map/xpf:string[@key="expert_validated"]/text()},
attribute musique-danse-machine {$map/xpf:string[@key="musique_danse_machine"]/text()},
attribute packed {$map/xpf:string[@key="_packed_id"]/text()},
element author {$map/xpf:string[@key="author"]/text()},
element title {$map/xpf:string[@key="title"]/text()},
element date {$map/xpf:string[@key="date_de_creation"]/text()},
element url {$map/xpf:string[@key="url"]/text()},
element alternative-title {$map/xpf:string[@key="alternative-title"]/text()},
element genre {$map/xpf:string[@key="genre"]/text()},
element acts {$map/xpf:string[@key="acts"]/text()},
element style {$map/xpf:string[@key="prose_vers"]/text()},
element prologue {$map/xpf:string[@key="prologue"]/text()}
}
};
let $base-url := "http://api.cfregisters.org/"
let $plays-url := $base-url || "plays"
let $plays := fetch:text($plays-url)
let $raw-xml := fn:json-to-xml($plays)
return
let $plays := element plays {
for $map in $raw-xml/xpf:array/xpf:map
return local:convert-play($map) (: db:add($db, local:convert-play($map), $map/xpf:number[@key="id"]/text() || ".xml") :)}
return csv:serialize($plays, map { 'header': true() })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment