0

How can we Update the variable value in a loop after the return?

let $daysList := (1 to functx:days-in-month(xs:date(xs:date(current-date()))))
let $temp:=0
for $day in $daysList
for $x in doc("booking2.xml")/bookings/booking
    where $x/roomNo = 301
    let $CheckInday := day-from-date(xs:date($x/checkInDate))
    let $CheckOutday := day-from-date(xs:date($x/checkOutDate))
    let $daysDiff := xs:date($x/checkOutDate) - xs:date($x/checkInDate)
    let $daysFromDuration := days-from-duration(xs:dayTimeDuration(xs:dayTimeDuration($daysDiff)))+1
    return 
       if($day >= $CheckInday and $day <= $CheckOutday) then
          if( $day = $CheckInday ) then
             let $temp = 1;
             <td colspan="{$daysFromDuration}" class="btn-success text-center">
               {
                   $x/custFirstName
               }
              </td>

           else ''
        else
          <td>{$day}</td>

In Above code, I want to update the value of $temp variable. Right now it's not allowed me to do this

let $temp := 1;

Is there any Idea to achieve these.

Thanks In Advance.

Ganesh
  • 81
  • 1
  • 12
  • As XQuery is a function language, you cannot update values of variables. – Before I can give you a reasonable solution, please revise your example, and remove variables that are not declared $CheckInday, etc.). Right now, it’s not clear what you will do with $temp, and if it is required at all. – Christian Grün Aug 21 '18 at 10:28
  • Hi, @ChristianGrün I have updated my code in question. Thanks for your quick response – Ganesh Aug 21 '18 at 10:46
  • Please try again. $day is undeclared. And I can’t see what you want to do with the result of $temp. – Christian Grün Aug 21 '18 at 10:49
  • I am sorry. Now it is okay? – Ganesh Aug 21 '18 at 10:53
  • Hi @ChristianGrün can we talk personally? need your little bit help. – Ganesh Mulay Aug 21 '18 at 17:43
  • Hi Ganesh, if you have questions regarding BaseX, feel free to subscribe to and write to our https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk mailing list. – Christian Grün Aug 21 '18 at 17:50

1 Answers1

1

It looks to me as if you're trying to imitate the common technique in procedural programming languages where you write a loop over some input collection that does two things: it generates some output corresponding to selected items in the input, and it sets a flag to indicate that it found some condition in the course of processing. Of course "setting a flag" is impossible in a functional programming language where variables are immutable.

Usually the best way of handling this is to separate the two operations. As far as I can see, you want to set your flag if there is some $booking in doc("booking2.xml")/bookings/booking and some $day in $daysList such that the $day is the same as the checkInDate of the $booking, that is

let $flag := some $x in doc("booking2.xml")/bookings/booking, $day in $days
    satisfies $x/roomNo = 301 and $day eq day-from-date(xs:date($x/checkInDate))

Now you might feel that (a) this involves duplicated source code, or (b) that it involves computing the same thing repeatedly. You can usually solve these problems by putting duplicated code into functions or variables. Don't worry about performance unless you actually have a performance problem.

If it turns out that you really do have a performance problem, and that computing both results in a single pass over the input will help to solve the performance problem, then you can modify the code to compute a composite result in a single pass, and then extract the parts of the composite result. The composite result might use an XML structure, or in XQuery 3.1 it might be a map.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Thanks, @Michael I will try this answer – Ganesh Aug 21 '18 at 11:48
  • Hi @Michael I tried this your solution but it gives me this error exerr:ERROR org.exist.xquery.XPathException: err:XPST0003 expecting "return", found '}' [at line 56, column 33] – Ganesh Aug 21 '18 at 12:06
  • That's a very basic syntax error, which I can't help you with without seeing your code. – Michael Kay Aug 22 '18 at 12:10