Created
April 28, 2015 17:21
-
-
Save joewiz/48ce061423aa7d3ada28 to your computer and use it in GitHub Desktop.
Construct a tab-separated value (TSV) file and save it to the file system, with XQuery and EXPath File Module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.0"; | |
(: Construct a tab-separated value (TSV) file and save it to the file system. | |
: | |
: This should work in any XQuery 3.0-enabled processor that supports the EXPath File | |
: Module. | |
: | |
: Tested in oXygen 16.1 in the "XPath/XQuery Builder" view, using the "Saxon-EE XQuery" | |
: engine, with "Enable XQuery 3.0 support" option selected (in the "Saxon-HE/PE/EE" | |
: portion of oXygen's preferences). | |
: | |
: @see http://expath.org/spec/file | |
:) | |
declare namespace file="http://expath.org/ns/file"; | |
let $cell-separator := '	' (: tab :) | |
let $row-separator := ' ' (: newline :) | |
let $column-headings := ('Document Number', 'Date', 'Document Type', 'People') | |
let $heading-row := string-join($column-headings, $cell-separator) | |
let $body-rows := | |
for $doc in doc('file:/Users/joe/Downloads/Series_IIa.xml')/PAL/doc | |
(: Each "cell" should be a string :) | |
let $docNumber := $doc/@docNumber/string() | |
let $date := $doc/Header/docDateInfo/@value/string() | |
let $docClass := $doc/@docClass/string() | |
let $people := normalize-space(string-join($doc/Header/listPerson/person/persName ! concat(./string(), ' (', ./@refurl, ')'), '; ')) | |
let $cells := ($docNumber, $date, $docClass, $people) | |
return | |
string-join($cells, $cell-separator) | |
let $all-rows := ($heading-row, $body-rows) | |
let $tsv := string-join($all-rows, $row-separator) | |
let $file-path := 'file:/Users/joe/Downloads/Series_IIa.tsv' | |
return | |
(: http://www.w3.org/TR/xquery-30/#id-try-catch :) | |
try { | |
( | |
file:write-text($file-path, $tsv), | |
concat('Saved ', count($all-rows), ' lines to ', $file-path) | |
) | |
} | |
catch * { | |
string-join( | |
($err:code cast as xs:string, $err:description, $err:value, concat("Module: ", $err:module, " (line ", $err:line-number, ", column ", $err:column-number, ")")), | |
'. ' | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment