سؤال

I have attached an xml document called hamlet.xml found here (http://www.ibiblio.org/xml/examples/shakespeare/hamlet.xml).

I am looking to get the following output using xquery:

   <speaker name="BERNARDO" lines="38"/>
    <speaker name="FRANCISCO" lines="10"/>
    <speaker name="HORATIO" lines="291"/>
    ...and so on for all the distinct speakers.

I have been able to get the name of the speaker using

========================

for $s in distinct-values(doc("hamlet")//SPEAKER
return <speaker name="$s}" />

=========================

But I don't know how to pull up the lines. Any help will be appreciated.

هل كانت مفيدة؟

المحلول

Simply enough:

for $speaker in
  distinct-values(//SPEAKER/text())
return
  <speaker
     name="{$speaker}"
     count="{count(//LINE[../SPEAKER=$speaker])}"/>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top