新加入Xquery的人,想为库模块制作函数

问题描述 投票:0回答:1

我正在做一个xquery任务,我很困惑,因为我正在为以后的xqueries做一个库模块。目前我被卡住的步骤是我需要创建一个名为GetAthleteMedals的函数,该函数将返回一个指定运动员赢得的奖牌,并带有一个名为aID的单一参数,该参数存储了一个运动员的id,然后在该函数中,我需要做一个FLOWR查询,该查询将从奖牌文档中查询每一个奖牌,其athID等于aID参数的值。

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>
};

medals.xml结构的例子

<medals>
   <medal athID="A3577" eventID="E6" olympicID="Summer1988" place="2-Silver"/>

athletes.xml结构示例

<athletes>
   <athlete athID="A100" country="MAR" gender="Male" name="ACHIK, Mohamed"/>
xml xquery
1个回答
2
投票

这里有不少问题。

let $athleteId := doc('athletes.xml')//athlete[athID=$aID]
  • athID 是一个属性,所以需要写成这样的文字。@athID

  • 变量是运动员元素,而不是ID值,所以变量名选得不好。

    return $medals[@medalId=$athleteId/@medalId]
  • 既不是 athlete 也不 medal 拥有 @medalId 属性;两者都有一个 @athID 属性。
<athIDMedals medal="athID">{
         for $medals in doc('medals.xml')/medals/medal
           where $medals/medal='athID'
           return $medal
           }</athIDMedals>
  • 我不清楚你打算让函数返回什么。是否如你的 return 的条款?还是新造的 athIDMedals 对象。目前,你似乎试图同时返回两个对象,这不会有好的效果。

  • 你需要在 medal="{$athleteID}" (我猜你要的价值在这里)

  • $medals/medal='athID'$medals 变量拥有一组 medal 元素。A medal 元素没有一个叫 medal如果是这样的话,奖牌元素永远不会等于字面的字符串'athID'。

  • return $medal 没有称为 $medal.

© www.soinside.com 2019 - 2024. All rights reserved.