I'm a newbie to XQuery and XPath. Posted below question over at Oxygen, but
didn't yet get the reply I wanted :-(
I'm trying to compare two XML structures. The problem I now have: What exactly
makes an XPath (string?)?
I have the following code:
declare variable $x1 := <top>
declare variable $x2 := <top>
for $node in $x1/rec
return for $elem in $node/
return if ($elem = $x2/rec/)
So far so good, but it took me a while to get there. Specifically, in my
original trials, I had something like
return if ($elem = $x2/rec/local-name($elem))
What confuses me:
My programming background is mostly perl, where I would happily expect a
string expansion before evaluation. But if this is not the case in XQuery: Why
does the interpreter not bark on the apparently useless part of the path
The question is really two-fold:
Thanks for your help!
This forum is intended for questions specific to the Saxon product: a good
place for general XSLT and XPath coding help is the xsl-list at
mulberrytech.com (although you're actually in XQuery territory here, for which
there is another list - talk at x-query.com).
The "/" operator means "evaluate the rhs operand once for every item in the
sequence produced by evaluating the lhs operand, with that item as the context
means evaluaye local-name($elem) once for every element in $x2/rec. There is
only one element in $x2/rec, so local-name($elem) gets evaluated exactly once,
and returns, of course, the local name of the node in variable $elem. It's
usually rather pointless to have an expression on the rhs of "/" than doesn't
depend on the context item, but it's not banned: there are plenty of
expressions that are pointless, like $x * 1, and it's a general principle of
modern language design that being pointless is not in itself justification for
making something illegal.
You were thinking, of course, that the local name of $elem would be used as a
NameTest in the next step of the path expression. Well, the language doesn't
work that way: you can't generate bits of syntax on the fly by evaluating
expressions, and then have that syntax parsed and evaluated again. It's
surprising what people sometimes expect: there was a post on the Wrox forum
today where someone wanted to use a variable containing the string "and" or
"or" in place of the actual operator. I guess there are some completely
interpretive shell languages that allow that, but XPath isn't one of them.
thanks for your detailed reply. Your rhs/lhs explanation makes a lot more
sense than much of what I've read online and in the XQuery book :-). Once such
conceptual things are clear, the rest probably goes down more easily ...