MakeMusic
SmartMusic Finale Garritan MusicXML

Updated XQuery Examples

Since XQuery was in the working draft stage in 2002, any attempt to publish an XQuery example was likely to become incorrect sooner rather than later. This was 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 conform 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>

clear

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>
            }

Prev