New to Xquery trying to make function for library module

泄露秘密 提交于 2021-02-08 08:33:32

问题


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"/>

回答1:


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]
  • Neither 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.



来源:https://stackoverflow.com/questions/61398595/new-to-xquery-trying-to-make-function-for-library-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!