Skip to content

Instantly share code, notes, and snippets.

@joewiz
Last active June 2, 2017 20:12
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 joewiz/8d690d2cdb0c278be3f2ccfb4b49f262 to your computer and use it in GitHub Desktop.
Save joewiz/8d690d2cdb0c278be3f2ccfb4b49f262 to your computer and use it in GitHub Desktop.
Parse date range phrase like Jan 20-21, 2017 and Jan 1, 2016-Mar 2, 2017 into TEI `<date>` element with `@from`, `@to`
xquery version "3.1";
declare namespace tei="http://www.tei-c.org/ns/1.0";
import module namespace dates="http://xqdev.com/dateparser" at "/db/apps/tumblr/modules/date-parser.xqm";
import module namespace iu = "http://history.state.gov/ns/xquery/import-utilities" at "/db/import-utilities.xqm";
for $section in doc('/db/apps/administrative-history/timeline.xml')//tei:div
let $head := $section/tei:head
let $date-string := $head/string()
let $analyze := iu:analyze-date-string($date-string)
let $convert :=
if ($analyze/self::error) then
(
'Error: ' || $analyze/string()
)
else
try
{
let $dates := $analyze/descendant-or-self::date
return
if (count($dates) eq 1) then
<date xmlns="http://www.tei-c.org/ns/1.0" when="{dates:parseDate($dates)/string()}">{$date-string}</date>
else
<date xmlns="http://www.tei-c.org/ns/1.0" from="{dates:parseDate($dates[1])/string()}" to="{dates:parseDate($dates[2])/string()}">{$date-string}</date>
}
catch *
{
"Error parsing " || $date-string
}
return
if ($analyze/self::error) then () else
update value $head with $convert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment