How do I get BaseX to return multiple elements in a nested XQuery?

1.6k views Asked by At

BaseX is complaining about a nested query of mine. I do not understand why it cannot return multiple lines like it did in the first query. The error says, "Expecting }, found >" and the > it is referring to is the > after name under trips. It works fine if the } is after the close-bracket for id, but obviously, that's not what I want. Here is the query:

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

There are 1 answers

0
Jens Erat On

If you want to return multiple items, you need to encapsulate them in a sequence ($item1, $item2, ..., $itemnN). In your case:

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>
)

But I'm unsure whether this will do what you expected, or if you actually want to create one element set per trip. Then, you'd also have a single trip element for result and are not required to return a sequence (this is also what's the case in the outer flwor-loop, here the <user/> element encapsulates to a single element):

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>