[xquery-talk] Clarification on XQuery environment and variablebinding?

Michael Kay mhk at mhk.me.uk
Sat Jul 9 01:49:59 PDT 2005


Your query is legal - some of us tried quite hard to argue that it should be
an error, but it isn't. Note that you have three quite distinct variables,
they just happen to have the same name.

Michael Kay
http://www.saxonica.com/
 

> -----Original Message-----
> From: talk-bounces at xquery.com 
> [mailto:talk-bounces at xquery.com] On Behalf Of David Sewell
> Sent: 08 July 2005 15:49
> To: talk at xquery.com
> Subject: [xquery-talk] Clarification on XQuery environment 
> and variablebinding?
> 
> I'd like confirmation of what the correct way in XQuery is to 
> duplicate
> a common task like multiple replacement on a string. For example, this
> kind of thing in Perl:
> 
>         $string = "I am a string?";
>         $string =~ s/am/yam/;
>         $string =~ s/\?/!/;
>         print "$string";    # => "I yam a string!"
> 
> A naive XQuery programmer (me) wants to write this as
> 
> 	let $string := "I am a string?"
> 	let $string := replace($string, "am", "yam")
> 	let $string := replace($string, "\?", "!")
> 	return $string    (: => "I yam a string!" :)
> 
> and in fact this produces the desired output in my two XQuery 
> processors
> (Saxon 8b, Mark Logic server). But my understanding of XQuery language
> syntax is that it is not legal to redefine a variable in the 
> same scope.
> 
> Section 2.1.4, "2.1.4 Notations for environments" in the 
> XQuery 1.0 and
> XPath 2.0 Formal Semantics documents:
> 
>   http://www.w3.org/TR/2005/WD-xquery-semantics-20050603/#N105A1
> 
> suggests that the proper way to construct the preceding would be
> 
> 	let $string := "I am a string?" return
> 	let $string := replace($string, "am", "yam") return
> 	let $string := replace($string, "\?", "!")
> 	return $string
> 
> where each "let" statement updates the environment and therefore
> creates a new $string variable each time.
> 
> So what I'm wondering is whether the first XQuery expression 
> without the
> nested "return"s is legal or not; if it's not legal, presumably the
> parsers are being merciful rather than throwing a static error?
> 
> DS
> 
> -- 
> David Sewell, Editorial and Technical Manager
> Electronic Imprint, The University of Virginia Press
> PO Box 400318, Charlottesville, VA 22904-4318 USA
> Courier: 310 Old Ivy Way, Suite 302, Charlottesville VA 22903
> Email: dsewell at virginia.edu   Tel: +1 434 924 9973
> Web: http://www.ei.virginia.edu/
> _______________________________________________
> talk at xquery.com
> http://xquery.com/mailman/listinfo/talk
> 




More information about the talk mailing list