[xquery-talk] Clarification on XQuery environment and variable binding?

David Sewell dsewell at virginia.edu
Fri Jul 8 11:48:57 PDT 2005

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:


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?


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/

More information about the talk mailing list