Database Reference
In-Depth Information
<h1>
{
$
page-title
}
</h1>
<ul>
{
for
$
play
in
$
play-info
/
play
return
<li>
{
string
(
$
play
)}
(
{
string
(
$
play
/
@name
)}
)
</li>
}
</ul>
</body>
</html>
And in the browser, you should see the output in
Figure 3-5
.
Figure 3-5. Our HTML page with a list of plays
Those new to XQuery might wonder why on earth our HTML code (the part that
starts with
<html>
) is now suddenly underneath a
return
statement. This is because
we introduced a local variable (
let $play-info :=
…). As soon as you do this, your
code becomes an XQuery FLWOR expression and needs a
return
statement for the
part you want to return.
Analyzing the Plays
Now assume we're a famous play director and in need of some data about the play.
Among the many, many questions directors struggle with are “Which character has
the most lines?” and “How many actors do I need?” Let's find out the answers using
XQuery.
Assume we have the
document-node
for the play's XML to analyze in a variable called
$play-document
. Subproblems to solve for our analysis are:
• We need a list of characters who speak. If you look inside the play's XML, you'll
see that everything said is inside a
SPEECH
element with a
SPEAKER
subelement.
So, to get a list of different speakers, all you have to do is get all the different val‐
ues of
SPEECH/SPEAKER
and filter them for uniqueness:
distinct-values
(
$
play-document
//
SPEECH
/
SPEAKER
)
Search WWH ::
Custom Search