[xquery-talk] Setting global variables

Michael Kay mike at saxonica.com
Wed Oct 31 21:52:57 PST 2007

> Is the whole point of functional programming to allow better 
> optimization by the processor / compiler? Or are there other 
> advantages as well?

That's part of the point. The other part is that it removes a lot of the
potential for coding errors. Programs without side-effects are much less
likely to contain bugs.
> As I mentioned to John, I can't do this because the document 
> name can change. If I could pass the document name from a 
> point external to the library, this would solve my problem. 
> (I suppose I should check the Saxon documentation and see if 
> this is possible...)

Sure, use an external variable

declare variable $docname external;
declare variable $doc := doc($docname);
declare variable $mydata := $doc//item[color='pink']

There's a Saxon extension that allows you to provide a default value for an
external variable.

(For the special case of document URIs, there's also another option, which
is to use a URIResolver. If you supply a URIResolver, then the literal URIs
contained in the query become abstract URIs that can be mapped to any
resources your application chooses.)

Michael Kay

> > Incidentally, the problem of passing parameters through 
> functions that 
> > don't actually look at them directly sounds like a request for 
> > something like XSLT 2.0's tunnel parameters.
> It does.
> > It looks as if XQuery is going to be extended to turn it into a 
> > procedural language through "scripting extensions". As far as I'm 
> > concerned, that's a great shame - being forced to learn to use 
> > functional programming has made me a much better programmer.
> How so?
> I've had to jump through a slightly different set of hoops, 
> but it all feels about the same as performing structured 
> programming. By "structured programming", I mean breaking 
> your application into many small functions and only passing 
> to each function the information it needs. This as opposed to 
> 70s-style spaghetti code, with lots of global variables and 
> GOTOs. That was a big difference for me and led to much cleaner code.
> Of course, here I am asking for global variables :)
> -- Ron
> _______________________________________________
> talk at x-query.com
> http://x-query.com/mailman/listinfo/talk

More information about the talk mailing list