Reputation: 13
i'm working on a xquery assignment and i'm confused as to whether and im doing it right i'm making a library module for later xqueries. the current step i'm stuck on is i need to create a function named GetAthleteMedals that return the medals won by a specifc athlete with a single parameter named aID that stores a althletes id then within that function i need to make a FLOWR query that interates through every medal from a medals document whose athID equals the value of the aID parameter.
xquery version "1.0";
module namespace olym="http://www.example.com/olympics";
declare variable $olym:athletes := doc('athletes.xml')/athletes/athlete;
declare variable $olym:discipline := doc('discipline.xml')/disciplines/discipline;
declare variable $olym:events := doc('events.xml')/events/event;
declare variable $olym:medals := doc('medals.xml')/medals/medal;
declare variable $olym:sports := doc('sports.xml')/sports/sport;
(:
New Perspectives on XML, 3rd Edition
Tutorial 9
Case Problem 3
Library module for olympics queries
Author: Zavier Vaidya
Date: 4/21/20
Filename: olym_functions.xqm
:)
declare function olym:GetAthleteMedals($aID as xs:string) as element()*
{
let $athleteId := doc('athletes.xml')//athlete[athID=$aID]
let $medals := doc('medals.xml')//medal
return $medals[@medalId=$athleteId/@medalId]
<athIDMedals medal="athID">{
for $medals in doc('medals.xml')/medals/medal
where $medals/medal='athID'
return $medal
}</athIDMedals>
};
example of medals.xml structure
<medals>
<medal athID="A3577" eventID="E6" olympicID="Summer1988" place="2-Silver"/>
example of athletes.xml structure
<athletes>
<athlete athID="A100" country="MAR" gender="Male" name="ACHIK, Mohamed"/>
Upvotes: 1
Views: 54
Reputation: 163458
There are quite a few things wrong here.
let $athleteId := doc('athletes.xml')//athlete[athID=$aID]
athID
is an attribute so it needs to be written @athID
the variable is an athlete element, not an ID value, so the variable name is poorly chosen.
return $medals[@medalId=$athleteId/@medalId]
athlete
nor medal
has an @medalId
attribute; both have an @athID
attribute.<athIDMedals medal="athID">{
for $medals in doc('medals.xml')/medals/medal
where $medals/medal='athID'
return $medal
}</athIDMedals>
It's not clear to me what you are intending the function to return. Is it a medal from the input document, as implied by your return
clause? Or is it a newly constructed athIDMedals
object. At present you seem to be trying to return both, which isn't going to work well.
You need curly braces in medal="{$athleteID}"
(I'm guessing what you want the value to be here)
$medals/medal='athID'
The $medals
variable holds a set of medal
elements. A medal
element doesn't have a child called medal
, and if it did, a medal element would never be equal to the literal string 'athID'.
return $medal
there's no variable called $medal
.
Upvotes: 2