Skip to content

Instantly share code, notes, and snippets.

View joewiz's full-sized avatar

Joe Wicentowski joewiz

  • Arlington, Virginia
View GitHub Profile
@joewiz
joewiz / secretary-of-state-travels-sorted-by-duration.xq
Created April 2, 2015 16:08
Travels of the Secretary of State, sorted by trip duration (longest to shortest), using XQuery
xquery version "3.0";
for $trip in collection('/db/cms/apps/travels/data/secretaries')/trips/trip
let $duration := xs:date($trip/end-date) - xs:date($trip/start-date)
order by $duration descending
return <trip duration="{$duration}">{ $trip }</trip>
@joewiz
joewiz / payload.xq
Created May 10, 2015 07:23
Receive payloads from BitBucket.org POST hooks with eXist
xquery version "3.1";
(:
Receive payloads from BitBucket.org POST hooks with eXist
Prerequisites:
1. A post-January 2015 build of the develop branch of eXist, which adds support
for XQuery 3.1's JSON parsing and serialization features.
@joewiz
joewiz / make-xquery-executable.xq
Created May 11, 2015 16:48
Make an XQuery file executable in eXist
let $path-to-xq := '/db/cms/apps/tei-content/views/ebook-batch.xq'
return
sm:chmod(xs:anyURI($path-to-xq), 'rwxr-xr-x')
@joewiz
joewiz / clean-excel-table.xq
Last active August 29, 2015 14:22 — forked from CliffordAnderson/build-ead.xqy
@CliffordAnderson's clean-excel-table.xqy, adapted for use with eXist 3.0 (develop branch)
xquery version "3.1";
declare namespace html="http://www.w3.org/TR/REC-html40"; (: the version of HTML used by the URL below :)
declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization"; (: use XQuery 3.1's JSON serialization :)
import module namespace http="http://expath.org/ns/http-client"; (: @see http://expath.org/spec/http-client :)
declare option output:method "json";
declare option output:media-type "application/json";
@joewiz
joewiz / parse-tweets.xq
Last active October 4, 2015 01:28
Parse and transform tweets using XQuery 3.1's JSON support
xquery version "3.1";
(: parse tweets using XQuery 3.1's JSON support
: see http://www.w3.org/TR/xpath-functions-31/#json
: sample JSON from https://dev.twitter.com/rest/reference/get/statuses/user_timeline
:)
let $json := json-doc('/db/user_timeline.json')
let $tweets := $json?*
return
@joewiz
joewiz / html5-serialization-prolog.xq
Created July 9, 2013 18:17
XQuery 3.0 HTML5 Serialization Prolog
xquery version "3.0";
declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method "html5";
declare option output:media-type "text/html";
@joewiz
joewiz / principal-officers-since-carter.xq
Last active December 20, 2015 10:19
Principal Officers who were serving on, or began service after, January 20, 1977, and who are still alive; using XQuery
xquery version "3.0";
(: Display a list of Principal Officers who are still alive who began serving on/after, or were serving as of, January 20, 1977. :)
let $all-people := collection('/db/cms/apps/principals-chiefs/data/')/person
let $principals := $all-people[.//role/@class='principal']
let $cutoff-date := '1977-01-20'
let $since-cutoff := $principals//role[@class='principal'][event[@type=('appointed', 'appointterminated')]/@when ge $cutoff-date]/ancestor::person
let $still-living := $since-cutoff[death/@type = 'unknown' or death = '' or empty(death/node())]
return
@joewiz
joewiz / fix-name-capitalization.xq
Last active December 20, 2015 16:18
Fix problems with mis-capitalized names, with XQuery
xquery version "3.0";
declare namespace fn="http://www.w3.org/2005/xpath-functions";
(: Fix problems with mis-capitalized names. For example:
Before: MACARTHUR, Douglas II
After: MacArthur, Douglas II
:)
declare function local:fix-name-capitalization($name as xs:string) {
(:
@joewiz
joewiz / get-tei-articles-collection-summary.xq
Last active December 20, 2015 17:29
Find the shortest and longest article in a collection of TEI XML articles by word count, and calculate the average word count, using XQuery
xquery version "3.0";
(: find the shortest and longest article and get the average word count of a collection of TEI XML articles :)
declare namespace tei="http://www.tei-c.org/ns/1.0";
(: in our case, 'articles' are TEI divs that have @xml:id attributes and no child divs;
we filter out the foreward since they're not full articles. :)
let $milestone-articles := collection('/db/cms/apps/tei-content/data/milestones')//tei:div[@xml:id and not(.//tei:div)][@xml:id ne 'foreword']
let $article-infos :=
@joewiz
joewiz / principals-and-chiefs-most-postings.xq
Created September 5, 2013 13:16
Show ambassadors in order of the most numbers of postings, using XQuery
xquery version "3.0";
for $ambassador in collection('/db/cms/apps/principals-chiefs/data')/person
let $postings := $ambassador/role
let $how-many-postings := count($postings)
group by $how-many-postings
order by $how-many-postings descending
return
<group postings="{$how-many-postings}" people-in-this-group="{count($ambassador)}">
{