0

When writing this code for Basex

<tourism>
{
  for $hotel in doc("tourism.xml")/tourism/hotel
  let $r:=$hotel/room
  let $g:=doc("tourism.xml")/tourism/guest
  where $g/@name="Udo Lindenberg" and $g/customer/@hotel=$hotel/@id and $r/@id=$g/occupies/@room
  return
    <hotel>
        <hotel_name>{string($hotel/@name)}</hotel_name>
          <first_day>$g/occupies/@from</first_day>
          <last_day>$g/occupies/@to</last_day>
    </hotel>
}
</tourism>

It returns this:

<tourism>
  <hotel>
    <hotel_name>Interconti</hotel_name>
    <first_day>$g/occupies/@from</first_day>
    <last_day>$g/occupies/@to</last_day>
  </hotel>
  <hotel>
    <hotel_name>Tourist Inn</hotel_name>
    <first_day>$g/occupies/@from</first_day>
    <last_day>$g/occupies/@to</last_day>
  </hotel>

How to return "2004-03-21" "2004-05-05", Instead of $g/occupies/@from $g/occupies/@to ?

  • 2
    Please add a small sample test data set, i.e. your `toursim.xml`. Without data, we can't help you much. However, the error message already pretty much tells you all about it: In your where clause you use a path expression (i.e., `/`), but at least one of this expressions does not return a node. And you can't use path expressions on strings, they only make sense on nodes. – dirkk Nov 17 '14 at 15:35
  • Also, I would strongly suggest you rephrase the title, because it sounds a lot like BaseX is at fault here (i.e. somehow misbehaving or contains a bug), while clearly your code is simply not valid XQuery. – dirkk Nov 17 '14 at 15:36
  • Please have a look at [ask] and [how to post an SSCCE](http://www.sscce.org). So far, your questions are lacking basic information. – Jens Erat Nov 17 '14 at 16:33
  • Ok, I will take these notes into consideration next time. @dirkk thanks. – David Nishnianidze Nov 17 '14 at 19:14
  • Looks like you're missing the `doc` in `let $g:=("tourism.xml")/tourism/guest`. (Should be `let $g:=doc("tourism.xml")/tourism/guest`) Looks like you're also missing some `{}` in `first_day` and `last_day`. Even if those are all corrected, I don't think your XQuery is going to work. You should add an example of what output you're expecting so someone can suggest an alternative XQuery. – Daniel Haley Nov 17 '14 at 19:46
  • Thanks for good point I missed. Right now I get output like this: Interconti $g/occupies/@from $g/occupies/@to Tourist Inn $g/occupies/@from $g/occupies/@to Now I need to convert $g/occupies/@from into string, or int, which I don't know how to do – David Nishnianidze Nov 17 '14 at 19:53
  • As Daniel Haley correctly pointed out, you are missing braces `{}` around these expressions. You do use them for `hotel_name`, but not for the other two elements. – dirkk Nov 17 '14 at 21:15

1 Answers1

3

Try this. Missing {} in the return for first_day and last_day. Sample XML structure would be of great help, as suggested by @dirkk

<tourism>
{
  for $hotel in doc("tourism.xml")/tourism/hotel
  let $r:=$hotel/room
  let $g:=doc("tourism.xml")/tourism/guest
    where $g/@name="Udo Lindenberg" and $g/customer/@hotel=$hotel/@id and $r/@id=$g /occupies/@room
  return
  <hotel>
    <hotel_name>{string($hotel/@name)}</hotel_name>
      <first_day>{$g/occupies/@from}</first_day>
      <last_day>{$g/occupies/@to}</last_day>
  </hotel>
}
</tourism>
John
  • 2,820
  • 3
  • 30
  • 50