This is vexing - it seems as though the mechanism that provides the necessary "filtering" is the very thing that slows the execution down so much. This wouldn't have been obvious from the single example document I sent earlier, but each document stands alone: all of the searching and reference linking done for each TrackRelease in a NewReleaseMessage should only refer to other nodes in that same NewReleaseMessage.
In my query, I started out with "for $r in /ernm:NewReleaseMessage" and I used $r on the right hand side of the subsequent for statements. It seems like without that, the execution is quick, but all the results from every document are getting matched to each other. With it, the results are correct, but the execution time shoots way up. In case any of you still have any patience for this question (and thanks again for everything so far!), I've attached a small sample set of 6 documents. The desired number of results from the query is 70 (which is the number of TrackReleases from all the documents combined), and the query that I've adapted from Christian's ddex2.xq which returns the right number of results is the following:
declare namespace ernm = '
http://ddex.net/xml/ern/411';
(: declare context item := db:open('ddex'); :)
for $r in /ernm:NewReleaseMessage
for $party in $r/PartyList/Party[
PartyReference/text() =
$r/ReleaseList/TrackRelease/ReleaseLabelReference
]
for $track_release in $r/ReleaseList/TrackRelease[
ReleaseLabelReference/text() =
$r/PartyList/Party/PartyReference
]
for $sound_recording in $r/ResourceList/SoundRecording[
ResourceReference/text() =
$track_release/ReleaseResourceReference
]
for $release in $r/ReleaseList/Release[
ResourceGroup/ResourceGroup/ResourceGroupContentItem/ReleaseResourceReference/text() =
$track_release/ReleaseResourceReference
]
return <identity>
<isrc>{ $track_release/ReleaseId/ISRC/text() }</isrc>
<artist>{ fn:string-join($sound_recording/DisplayArtistName, '/') }</artist>
<title>{ $sound_recording/DisplayTitleText/text() }</title>
<album>{ $release/DisplayTitleText/text() }</album>
<icpn>{ $release/ReleaseId/ICPN/text() }</icpn>
<sublabel>{ $party/PartyName/FullName/text() }</sublabel>
</identity>