Skip to content

Instantly share code, notes, and snippets.

@cwulfman
Created April 21, 2017 16:21
Show Gist options
  • Save cwulfman/c006b358577c0d21eb029229fece637c to your computer and use it in GitHub Desktop.
Save cwulfman/c006b358577c0d21eb029229fece637c to your computer and use it in GitHub Desktop.
For Vanderbilt XQuery group: sample XQuery to retrieve data about musical notation in Blue Mountain
xquery version "3.1";
(:~
: A query to retrieve interesting data about
: music publication in the Blue Mountain
: data set.
:
: @author Cliff Wulfman
: @version 1.0
: April 21, 2017
:)
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare variable $collection := '/db/bmtn-data/transcriptions/periodicals';
(:~
: Isolate this primary look-up as a function so you could
: refine your query to a specific magazine, for example.
:)
declare function local:issues-with-music($collection as xs:string)
as node()*
{
collection($collection)/tei:TEI[.//tei:classCode[. = "Music"]]
};
(:~
: Collect various bits of data about an issue and return
: those bits as a simple record. The record can then be
: serialized or converted into TSV or CSV and imported
: into a spreadsheet or other analytical tool.
:)
declare function local:issue-music-stats($issue as node())
as node()
{
let $id := data($issue//tei:idno[@type='bmtnid'])
let $magazineid := data($issue//tei:relatedItem[@type='host']/@target)
let $pubDate :=
data($issue//tei:sourceDesc/tei:biblStruct/
tei:monogr/tei:imprint/tei:date/@when)
let $music-div-count := count($issue//tei:classCode[. = "Music"])
return
<issue>
<issueID>{ $id }</issueID>
<magID>{ $magazineid }</magID>
<pubDate>{ $pubDate }</pubDate>
<musicCount>{ $music-div-count }</musicCount>
<primaryLanguage>{ local:primary-language($issue) }</primaryLanguage>
</issue>
};
(:~
: What is the primary language of the magazine in which the musical notation appears?
: This is a surrogate question for several other questions: (1) Are their national
: variations in musical literacy? (2) Are there national or regional variations in
: publication practices, perhaps reflecting factors like printing cost, etc?
:
: This function assumes the primary language of an issue is that of the magazine as
: a whole; that data point is established editorially by library cataloguers. We
: could re-write the function to be more issue-specific, by counting and sorting the
: language elements in the issue itself, but that might be too localized. Either
: algorithm has its merits.
:)
declare function local:primary-language($issue)
as xs:string
{
let $magid := data($issue//tei:relatedItem[@type='host']/@target)
let $host := collection($collection)//tei:idno[.= $magid]/ancestor::tei:TEI
let $langs := $host//tei:langUsage/tei:language
return
if ($langs) then
data($langs[1]/@ident)
else "None"
};
<issues>
{
for $i in local:issues-with-music($collection)
return local:issue-music-stats($i)
}
</issues>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment