Recordare Main Navigation Menu Recordare

Updated XQuery Examples for MusicXML in Practice

Since XQuery is still at the working draft stage, any attempt to publish an XQuery example is likely to become incorrect sooner rather than later. This is true of our XQuery examples in the MAX 2002 paper, MusicXML in Practice: Issues in Translation and Analysis.

Here are versions of the XML queries in that paper that conforms to the November 15, 2002 XQuery working draft. They work correctly with the Galax 0.3.0 implementation from Lucent -Bell Labs and AT&T Labs Research.

Pitch range query:

define function MidiNote($thispitch as element) as xs:integer
{
  let $step := $thispitch/step
  let $alter :=
    if (empty($thispitch/alter)) then 0
    else xs:integer($thispitch/alter)
  let $octave := xs:integer($thispitch/octave)
  let $pitchstep :=
    if ($step = "C") then 0
    else if ($step = "D") then 2
    else if ($step = "E") then 4
    else if ($step = "F") then 5
    else if ($step = "G") then 7
    else if ($step = "A") then 9
    else if ($step = "B") then 11
    else 0
  return 12 * ($octave + 1) + $pitchstep + $alter
} 
let $doc := document("MusicXML/frauenliebe8.xml")
let $part := $doc//part[./@id = "P1"]
let $highnote := max(for $pitch in $part//pitch return MidiNote($pitch))
let $lownote := min(for $pitch in $part//pitch return MidiNote($pitch)) 
let $highpitch := $part//pitch[MidiNote(.) = $highnote]
let $lowpitch := $part//pitch[MidiNote(.) = $lownote]
let $highmeas := string($highpitch[1]/../../@number)
let $lowmeas := string($lowpitch[1]/../../@number) 
return
  <result>
  <low-note>{$lowpitch[1]}
    <measure>{$lowmeas}</measure>
  </low-note>
  <high-note>{$highpitch[1]}
    <measure>{$highmeas}</measure>
  </high-note>
  </result>

Melody retrieval query:

<result>
{let $doc := 
    document("MusicXML/frere-jacques.xml")
let $notes := $doc//note
for $note1 in 
        $notes[string(./pitch/step) = "C"],
    $note2 in $notes[. >> $note1][1],
    $note3 in $notes[. >> $note2][1],
    $note4 in $notes[. >> $note3][1]
    let $meas1 := $note1/..
    let $part1 := $meas1/..
    let $part2 := $note2/../..
    let $part3 := $note3/../..
    let $part4 := $note4/../..
where string($note2/pitch/step) = "D"
  and string($note3/pitch/step) = "E"
  and string($note4/pitch/step) = "C"
  and (string($part1/@id) =
   string($part2/@id))
  and (string($part2/@id) =
   string($part3/@id))
  and (string($part3/@id) =
   string($part4/@id))
return 
  <motif>
       {$note1/pitch} {$note2/pitch}
       {$note3/pitch} {$note4/pitch}
       <measure>{$meas1/@number}</measure>
       <part>{$part1/@id}</part>
  </motif>
}
</result>

Home - Music - Software - MusicXML - Tutorial - Events - eConcertBand - Search - Store - About Us

Copyright © 2001-2003 Recordare LLC. All rights reserved.

Last updated April 5, 2003.