2016-12-25 62 views
1

我有这样开始的一个XML文件:的Xquery返回的XML元素

<tpinfos xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schemaTP.xsd"> 
<jour id="lundi"> 
    <salle id="A11"> 
     <creneau debut="8:00" fin="10:00"> 
      <formation>M1 Info</formation> 
      <enseignant>ME Voge</enseignant> 
      <matiere>ACT</matiere> 
     </creneau> 
     <creneau debut="10:00" fin="12:00"> 
      <formation>M1 Miage</formation> 
      <enseignant>AC Caron </enseignant> 
      <matiere>ED</matiere> 
     </creneau> 
     <creneau debut="12:00" fin="13:30"> 
      <formation>M1 Info</formation> 
      <enseignant>ME Voge</enseignant> 
      <matiere>ACT</matiere> 
     </creneau> 
     <creneau debut="15:20" fin="17:20"> 
      <formation>M1 Info</formation> 
      <enseignant>ME Voge</enseignant> 
      <matiere>ACT</matiere> 
     </creneau> 
    </salle> 

我应该采取一切<克勒诺>将匹配<形成>这个函数的参数:

declare function local:filtreFormation($f as xs:string) as element(tpinfos) 

我的问题是我应该返回的数据类型,“元素(tpinfos)”。 因为这个数据类型,我所有的尝试都失败了,我真的不明白我应该怎样做,而不会出错。

我最后的尝试是:

declare function local:filtreFormation($f as xs:string) as element(tpinfos) 
{ 
    for $cr in doc("planningTP.xml")/jour/salle/creneau 
    where $cr/formation = $f 
    return element tpinfos {$cr} 
}; 

但它返回的错误“错误的发生,以匹配所需的序列类型”

任何人都可以向我解释什么是错的吗?

回答

1

函数返回的sequence type的基数与您的结果不符。目前,它期望始终返回一个(并且只有一个)tpinfos元素。如果它返回一个空序列或多个元素,这些元素与签名不匹配并会导致错误。

改变你的函数声明为允许零到许多tpinfos元素:

declare function local:filtreFormation($f as xs:string) as element(tpinfos)*