I am using BaseX XML database.
I downloaded XML files by using the following URL: https://clinicaltrials.gov/ct2/download_studies?term=heart
It provided me with a search_result.zip file. I unzipped all 10,000 XML files into the following directory: e:\Temp\clinicaltrials.gov
After that I created a new BaseX database and named it 'clinicaltrials'. You can check it here: Reading Multiple XML Files with BaseX
While waiting for the clarifications on grouping, here is a relevant XQuery as a starting point. It is applying your first two requirements: "...Find the clinical studies of which the brief title contains a given phrase, from the results get those who have intervention type Drug..." via where
clause.
xquery version "3.1";
declare option output:omit-xml-declaration "no";
declare option output:encoding "UTF-8";
for $doc in collection('clinicaltrials')
let $t := $doc/clinical_study/brief_title
where $doc/clinical_study/brief_title[contains(.,'Heart')]
and $doc/clinical_study/intervention/intervention_type = 'Drug'
return $t
XQuery with grouping and ranking (top 10)
xquery version "3.1";
declare option output:omit-xml-declaration "no";
declare option output:encoding "UTF-8";
<root>
{
for $drug in collection('clinicaltrials')/clinical_study[brief_title[contains(.,'Heart')]]/intervention[intervention_type eq 'Drug']
let $drugName := fn:lower-case($drug/intervention_name)
group by $drugName
order by count($drug) descending, $drugName
count $rank
where $rank <= 10
return <drug>
<drug_name>{$drugName}</drug_name>
<counter>{count($drug)}</counter>
</drug>
}
</root>
Output
<?xml version="1.0" encoding="UTF-8"?>
<root>
<drug>
<drug_name>placebo</drug_name>
<counter>592</counter>
</drug>
<drug>
<drug_name>ivabradine</drug_name>
<counter>60</counter>
</drug>
<drug>
<drug_name>nesiritide</drug_name>
<counter>46</counter>
</drug>
<drug>
<drug_name>furosemide</drug_name>
<counter>40</counter>
</drug>
<drug>
...
</root>