[xquery-talk] Top N Most Common Mistakes

Michael Kay mike at saxonica.com
Thu Aug 2 18:05:18 PDT 2007


> 
> If it's not too much of a digression into CS theory, could 
> someone explain this point?

Not many people these days use macro-processing languages (see for example
http://www.ml1.org.uk/) but they have left a mark - they influenced the
design of text processing languages like sed and awk and later Perl, and the
use of the "$" to mark a variable comes from this language tradition. Shell
script languages also owe a lot to this heritage, as do languages (if you
can call them that) like "make". And of course we use $1 in a replacement
string in a regular expression - it shares the same history.

Quoting from http://www.apl.jhu.edu/Misc/Unix-info/make/make_6.html

<quote>
Variable references work by strict textual substitution. Thus, the rule

foo = c
prog.o : prog.$(foo)
        $(foo)$(foo) -$(foo) prog.$(foo)

could be used to compile a C program `prog.c'.
</quote>

People who have encountered this kind of language, and perhaps some who
haven't, will sometimes (consciously or unconsciously) expect XSLT and
XQuery to work this way. For example, they might write

let $x := 'title'
return $book/$x

and imagine that this has the same effect as writing $book/title.

Perhaps the most common example in XSLT is <xsl:sort select="$x"/> where $x
holds an element name.

It's not an unreasonable extrapolation from the fact that you can write <foo
bar="{$x}"/>. 

You will also see attempts to call a dynamically-determined function (or
XSLT template) by using $func(x) or <xsl:call-template name="$tname"/>, and
attempts to include a dynamically-determined stylesheet module using
<xsl:include href="$uri"/>. I've even seen someone try to construct an
element as <$elname/>.

(And of course people will try it with curly braces if it doesn't work
without them.)

It represents a profound conceptual misunderstanding about the nature of the
language - but not an unreasonable expectation, if you come at it from first
principles.

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







More information about the talk mailing list