Skip to content

Instantly share code, notes, and snippets.

@WaxCylinderRevival
Last active October 18, 2017 18:54
Show Gist options
  • Save WaxCylinderRevival/576e7d149f646cc109e6270e33e03d86 to your computer and use it in GitHub Desktop.
Save WaxCylinderRevival/576e7d149f646cc109e6270e33e03d86 to your computer and use it in GitHub Desktop.
Test of local function to find date string(s) with common FRUS date pattern and wrap with date element
import module namespace functx="http://www.functx.com" at "http://www.xqueryfunctions.com/xq/functx-1.0-nodoc-2007-01.xq";
(: declare namespace date-processing='https://history.state.gov/ns/xquery/dates' :)
declare function local:find-date-strings
( $textWithDates as xs:string? ) as element()* {
for $textString in
$textWithDates => data() => serialize() => normalize-space() => functx:get-matches('((\d{1,2}[(st)(d)(nd)(rd)(th)]*\s+(January|February|March|April|May|June|July|August|September|October|November|December),*\s+\d{4})|((January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2}[(st)(nd)(d)(rd)(th)]*,\s+\d{4}))') => serialize() => functx:trim() => tokenize('\s\s+')
return <date>{$textString}</date>
};
let $input := <head>The first date is <hi>February 2d, 1865</hi>. The next date is March 1, 2010. The <strong>third</strong> date is <hi rend="italic">31st July</hi> 2015.</head>
let $dates := local:find-date-strings($input)
return <results>{$dates}</results>
@WaxCylinderRevival
Copy link
Author

Without functx:trim, there's a leading space in the first entry:

screen shot 2017-10-18 at 12 36 39 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment