Updating variables in XQuery - is it possible or not?

I am a little confused regarding variable updates in XQuery: In [1] it says:

Variables cannot be updated. This means that you cannot write something like let $ x: = $ x + 1. This rule may seem very strange if you expect XQuery to behave just like procedural languages ​​like JavaScript. But XQuery is not that language, it is a declarative language and works at a higher level. There are no rules about the execution order of various expressions (which means that the small yellow triangle that shows the current execution point in the Stylus Studio XQuery debugger and the XSLT debugger can sometimes behave in amazing ways), which means that designs whose result will depend on execution order (e.g. variable assignment).

I am wondering if there is no way to reliably update a variable? Maybe I’m just used to these things in other languages, but I can’t imagine and don’t believe, -)

[1] http://www.stylusstudio.com/xquery_flwor.html , second paragraph below the screenshots of the chapter “L for LET”

UPDATE: I have to add the question to this: shouldn't the existing variable in the if statement be updated, because in this case the execution order is clear? Guess you're just not allowed to use something like $ x = $ x + 1 in a loop?

+5
source share
6 answers

, . ; ​​ , - .

. , .

, , . , . , :

- ?

for $v in //video
let $x := xs:int($v/runtime) * xdt:dayTimeDuration("PT1M")
return concat($v/title, ": ", 
      hours-from-duration($x), " hour(s) ",
      minutes-from-duration($x), " minutes")

( . , (PT1M), , . .)

$x XQuery . . , for , .

+5

, , XQuery XQuery Scripting Extension 1.0

, Zorba Sandbox:

declare namespace an = "http://zorba.io/annotations";

declare %an:sequential function local:fib(){
  variable $a as xs:integer := 0;
  variable $b as xs:integer := 1;  
  variable $c as xs:integer := $a + $b;
  variable $fibseq as xs:integer* := ($a, $b);
  while ($c < 100) { 
     $fibseq := ($fibseq, $c);
     $a := $b;
     $b := $c;
     $c := $a + $b; 
  } 
  $fibseq
};

local:fib()

. apply ( , ;) .

FLWOR, at:

for $item at $x in ("a","b","c")
return $x

:

<?xml version="1.0" encoding="UTF-8"?>
1 2 3
+6

if.

( , !).

XQuery ( - ) - .

for, , , , . , , .

. , . , .

headfuzz, ( ) ( ) .

, , , . , , , .

, , , " ", , for.

$patternsremaining - ( $pattern). , , $ .

(: Here $patterns looks like <pattern match="something" replace="else" /> :)
declare function local:transform($text as text(), $patterns as element(pattern)*) {
   if(not($patterns)) then 
      $text
   else
      let $patternsremaining := $patterns[position() > 1],
          $modifiedtext := replace($text, $pattern/@match, $pattern/@replace)
      return 
         if($local:language="French" and not($patterns[@match='le'])) then (
             local:transform($modifiedtext, ($patternsremaining, <pattern match="Londres" replace="London" />))
      )
      else(
         local:transform($modifiedtext, $patternsremaining)
      )
};

XSLT XQuery (, ) - , . , .

if() then() else(), , ( , " " ) , - , if , . , . !

, , , ( , ).

. , , , .

+2

, . , ?

, , "" , . , . sqrt() XSLT ( ) ...

0

, , .

So

let $x := $x + +1
return $x

let $x_2 := $x_1 +1
return $x_2
0

.

XQuery . / . , , concurrency. "" , concurrency. , Java, concurrency.

As in any other functional language, you can always provide "eval" for your "expr", and then make the necessary changes with a copy.

Please note that I am not saying that XQuery is a good functional programming language. Take a look at erlang for an example of a good functional language and implementation that provides good performance.

0
source

All Articles