Gdzie powinienem ograniczać moje wyniki?

Stworzyłem plik XML z listą kilku tysięcy wyszukiwanych haseł, które muszę wykonać na dokumencie. Następnie stworzyłem to zapytanie z przykładowego zestawu wyszukiwanych haseł, jako test, do wykonania na dokumencie testowym, z niektórymi próbkami z rzeczywistego dokumentu:

let $keywords := ("best clients", "Very", "20")
for $keyword in $keywords
let $matches := doc('test')/set/entry[matches(comment, $keyword, 'i')]
return (<re>
{subsequence($matches/comment, 1, 1),
subsequence($matches/buyer, 1, 1)}</re>,
<re>
{subsequence($matches/comment, 2, 1),
subsequence($matches/buyer, 2, 1)}
</re>
)

Próbuję wrócić<re><comment /><buyer /></re><re><comment /><buyer /></re>... continuous, ale przywracam je w porządku.

Jest to fragment z analizowanego dokumentu (usunąłem nazwy nabywcy i niektóre gniazda, aby ułatwić czytanie):

<set>
<entry>
<comment>The client is only 20 years old.  Do not be surprised by his youth.</comment>
<buyer></buyer>
<id>1282</id>
<industry>International Trade; Fish and Game</industry>
</entry>
<entry>
<comment>!On leave in October.</comment>
<buyer></buyer>
<id>709</id>
<industry>Real Estate</industry>
</entry>
<entry>
<comment>Is often !out between 1 and 3 p.m.</comment>
<buyer></buyer>
<id>127</id>
<industry>Virus Software Marketting</industry>
</entry>
<entry>
<comment>Very personable.  One of our best clients.</comment>
<buyer></buyer>
<id>14851</id>
<industry>Administrative support.</industry>
</entry>
<entry>
<comment>!Very difficult to reach, but one of our top buyers.</comment>
<buyer></buyer>
<id>1458</id>
<industry>Construction</industry>
</entry>
<entry>
<comment></comment>
<buyer></buyer>
<id>276470</id>
<industry>Bulk Furniture Sales</industry>
</entry>
<entry>
<comment>A bit of an eccentric.  One of our best clients.</comment>
<buyer></buyer>
<id>1506</id>
<industry>Sports Analysis</industry>
</entry>
<entry>
<comment>Very gullible, so please !be sure she needs what you sell her.  She's one of our best clients.</comment>
<buyer></buyer>
<id>1523</id>
<industry>International Trade</industry>
</entry>
<entry>
<comment>He wants to buy everything, but !he has a tight budget.</comment>
<buyer></buyer>
<id>1524</id>
<industry>Public Relations</industry>
</entry>
</set>

Niektóre słowa kluczowe używam: „Najlepszy klient *”, „Handel”, „20”, ....

byłem

Wyjściem jest długa lista wpisów z dziećmi komentarzy i kupujących jako rodzeństwem pod elementem wejściowym. Chciałbym ograniczyć liczbę zwracanych wpisów2 per keyword. Staram się także, aby priorytetem były komentarze zaczynające się od wykrzyknika (!).

Bieżące wyjście (zbliżanie się):

<re><comment>Very personable.  One of our best clients.</comment>
  <buyer/>
</re><re><comment>A bit of an eccentric.  One of our best clients.</comment>
  <buyer/>
</re><re><comment>Very personable.  One of our best clients.</comment>
  <buyer/>
</re><re><comment>!Very difficult to reach, but one of our top buyers.</comment>
  <buyer/>
</re><re><comment>The client is only 20 years old.  Do not be surprised by his youth.</comment>
  <buyer/>
</re><re/>

Aktualny format wyjściowy:

<entry>
<comment>keyworda</comment>
<buyer></buyer>
</entry>
<entry>
<comment>keyworda</comment>
<buyer></buyer>
</entry>
<entry>
<comment>keywordb</comment>
<buyer></buyer>
</entry>
<entry>
<comment>!keywordb</comment> //Not prioritized.
<buyer></buyer>
</entry>
<entry>
<comment>keywordc</comment>
<buyer></buyer>
</entry>

Pożądane wyjście:

<entry>
<comment>!keyworda</comment>
<buyer></buyer>
</entry>
<entry>
<comment>keyworda</comment>
<buyer></buyer>
</entry>
<entry>
<comment>!keywordb</comment>
<buyer></buyer>
</entry>
<entry>
<comment>!keywordb</comment>
<buyer></buyer>
</entry>

(Zasadniczo, priorytety dla wpisów zawierających wykrzykniki i ograniczenie wyników do 2 dla każdego słowa kluczowego.).

questionAnswers(1)

yourAnswerToTheQuestion