如何让BaseX在嵌套的XQuery中返回多个元素?

时间:2014-11-17 00:53:19

标签: xquery basex

BaseX抱怨我的嵌套查询。我不明白为什么它不能像在第一个查询中那样返回多行。错误上写着“期待”,发现>“它所指的>是旅行后姓名后的>。如果}位于id的close括号之后,它可以正常工作,但显然,这不是我想要的。这是查询:

for $u in doc("export.xml")/database/USERS/tuple
   return 
   <user>
   <login>{$u/USERNAME/text()}</login>
   <email></email>
   <name></name>
   <affiliation></affiliation>
   <friend></friend>
   <trip>
      {for $t in doc("export.xml")/database/TRIPS/tuple
      where $t/ADMIN/text() = $u/USERNAME/text()
      return 
      <id> {$t/ID/text()} </id>
      <name> {$t/NAME/text()} </name>       (: Error is here with <name> :)
      <feature> {$t/FEATURE/text()} </feature>
      <privacyFlag> {$t/PRIVACY/text() </privacyFlag>)
      }
   </trip>
</user>

1 个答案:

答案 0 :(得分:3)

如果您想要return多个项目,则需要将它们封装在序列($item1, $item2, ..., $itemnN)中。在你的情况下:

for $t in doc("export.xml")/database/TRIPS/tuple
where $t/ADMIN/text() = $u/USERNAME/text()
return (
  <id> {$t/ID/text()} </id>,
  <name> {$t/NAME/text()} </name>,
  <feature> {$t/FEATURE/text()} </feature>,
  <privacyFlag> {$t/PRIVACY/text() </privacyFlag>
)

但我不确定这是否符合您的预期,或者您是否真的希望每次创建一个元素集。然后,你还有result的单个trip元素,并且不需要返回一个序列(这也是外部flwor-loop的情况,这里<user/>元素封装到一个单个元素):

for $t in doc("export.xml")/database/TRIPS/tuple
where $t/ADMIN/text() = $u/USERNAME/text()
return
  <trip>
    <id> {$t/ID/text()} </id>
    <name> {$t/NAME/text()} </name>
    <feature> {$t/FEATURE/text()} </feature>
    <privacyFlag> {$t/PRIVACY/text() </privacyFlag>
  </trip>