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 / 01-secretary-of-state-travels-to-a-country.xq
Last active August 28, 2015 16:21
Travels of the Secretary of State, grouped by secretary, showing duration of visits in a country, using XQuery
xquery version "3.0";
let $results :=
for $trip in collection('/db/cms/apps/travels/data/secretaries')/trips/trip[country/@id = 'israel']
let $duration := days-from-duration(sum($trip ! (./xs:date(end-date) - ./xs:date(start-date) + xs:dayTimeDuration('P1D'))))
group by $secretary := $trip/name
return
<trips secretary="{$secretary}" trips="{count($trip)}" days="{$duration}">{
$trip !
<trip>{
@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 / 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 / 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 / string-to-sequence.xq
Created May 6, 2015 17:53
Turn a whitespace-delimited string of text into an XQuery-style sequence string
xquery version "3.0";
(: Sometimes you need to turn a list of words into a sequence-style string :)
declare function local:string-to-sequence($string as xs:string) as xs:string {
let $string := normalize-space($string)
let $items := tokenize($string, '\s')
let $quoted := $items ! concat("'", ., "'")
return
concat(
@joewiz
joewiz / generate-tsv.xq
Created April 28, 2015 17:21
Construct a tab-separated value (TSV) file and save it to the file system, with XQuery and EXPath File Module
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).
@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 / slowest-queries.xq
Created March 13, 2015 04:17
Examine query duration from JMX logs collected by monex #existdb
xquery version "3.0";
(: Examine query duration from JMX logs collected by monex :)
declare namespace jmx="http://exist-db.org/jmx";
declare function local:milliseconds-to-time($timestamp as xs:long) as xs:dateTime {
let $days := xs:int($timestamp div 1000 div 24 div 60 div 60)
let $remainder := $timestamp - ($days * 24 * 60 * 60 * 1000)
let $hours := xs:int($remainder div 1000 div 60 div 60)
xquery version "3.0";
module namespace r = "http://joewiz.org/ns/xquery/roman-numerals";
(: Converts standard Roman numerals to integers.
: Handles additive and subtractive but not double subtractive.
: Case insensitive.
: Doesn't attempt to validate a numeral other than a naïve character check.
: See discussion of standard modern Roman numerals at:
: http://en.wikipedia.org/wiki/Roman_numerals.
@joewiz
joewiz / de-redirect.js
Created March 2, 2015 21:29
de-redirect bookmarklet for inaccessible redirect servers, e.g., redirect.mycompany/?url=
javascript:(function(){window.location=loadTimeData.data_.summary.failedUrl.replace(/.*\?url=/,'');})()