There's nothing in the XPath/XQuery specs that would disallow $b/$a as an
expression, provided that $b returns a (possibly-empty) sequence of nodes. If
$a also returns nodes, then you can think of $b/$a as:
my:distinct-nodes(for $x in $b return $a)
Where my:distinct-nodes() is a function that removes duplicate nodes, since
that is what the "/" operator actually does. Looking at this example again:
let $a := /article
return fn:collection()/$a/title
$a is bound to zero or more <article> elements. It still returns the same value
even if you put a step to its left (e.g., "fn:collection()"); there is no
dynamic expression evaluation going on here even if it might have appeared that
way. The following query, for example, will still return the same value:
let $a := /article
let $b := <foo/>
return $b/$a/title
However, if $b (or fn:collection()), returns empty, then $a won't get evaluated
and the overall result will be an empty sequence.
Also, the semantics are different if the last step ($a) returns atomic values:
let $a := "bar"
let $b := (<foo/>, <foo/>, <foo/>)
return $b/$a
In that case, we're not dealing with nodes, so we're not removing any
duplicates and the result is ("bar", "bar", "bar").
Hope that helps!
Evan Lenz
Software Developer, Community
MarkLogic Corporation
Phone +1 360 297 0087
email [email protected]<mailto:[email protected]>
web developer.marklogic.com<http://developer.marklogic.com/>
From: Danny Sokolsky
<[email protected]<mailto:[email protected]>>
Reply-To: General MarkLogic Developer Discussion
<[email protected]<mailto:[email protected]>>
Date: Wed, 9 Feb 2011 14:52:50 -0800
To: General MarkLogic Developer Discussion
<[email protected]<mailto:[email protected]>>
Subject: Re: [MarkLogic Dev General] using XPath with no context
I don’t know why the collection()/$a/title is working…I would have expected a
syntax error there--I’ll look into that.
As for the context nodes, let me qualify what I said before. In 1.0-ml, the
whole database is the context. In 1.0, there is no default context node. The
spec does not allow for the whole database to be the context node—it requires a
single node. In MarkLogic, it does not make sense to choose a single node as
the context node, but it does make sense to choose the database as the context
(“context nodes”, if you will). That is why there is a difference
between1.0-ml and 1.0 here.
You are correct in thinking that explicitly supplying the context is a good
idea, as then that code will run in both 1.0 and 1.0-ml, and it will be clearer
what the intent is too.
Hope that helps,
-Danny
From:
[email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of
[email protected]<mailto:[email protected]>
Sent: Wednesday, February 09, 2011 2:25 PM
To: [email protected]<mailto:[email protected]>
Subject: Re: [MarkLogic Dev General] using XPath with no context
Ok so it uses the default context is none is specified. but I still don't
understand why this works:
xquery version '1.0-ml';
let $test-doc :=
<article>
<title>Hello</title>
</article>
return xdmp:document-insert("/test-doc.xml", $test-doc)
;
let $a := /article
return fn:collection()/$a/title
=> <title>Hello</title>
But I think I will always specify a context anyway.
And I guess it is just for the 1.0-ml dialect
> From: [email protected]<mailto:[email protected]>
> To: [email protected]<mailto:[email protected]>
> Date: Wed, 9 Feb 2011 14:13:39 -0800
> Subject: Re: [MarkLogic Dev General] using XPath with no context
>
> In MarkLogic, the default context is all of the documents in the database to
> which your App Server is configured. So fn:collection() is equivalent to the
> default context.
>
> -Danny
>
> -----Original Message-----
> From:
> [email protected]<mailto:[email protected]>
> [mailto:[email protected]] On Behalf Of G. Ken Holman
> Sent: Wednesday, February 09, 2011 1:28 PM
> To: General MarkLogic Developer Discussion
> Subject: Re: [MarkLogic Dev General] using XPath with no context
>
> At 2011-02-09 14:21 -0700, [email protected]<mailto:[email protected]>
> wrote:
> >What are the implications of doing:
> >
> >/article
>
> That is valid only if the processor gives you a source node tree at
> invocation time.
>
> >vs
> >
> >fn:collection()/article
>
> fn:collection() returns document nodes, so there is no "missing context".
>
> >or
> >
> >fn:doc()/article
>
> fn:doc() returns a document node, so there is no "missing context".
>
>
> >?
> >
> >Seems like the first one shouldn't be allowed since no context is
> >specified, yet it seems to work. Is that part of the XQuery spec or
> >is that part of the 1.0-ml dialect? (seems so because when I specify
> >1.0 I get an error). What is happening behind the scenes when no
> >context is specified?
>
> All depends on the processor. Saxon is an XQuery processor that
> accepts a source document specification on the command line and so
> "/article" is in the context of the document node of that source
> document. If you run Saxon without supplying a source document, you
> get an error.
>
> A processor defines what is in the dynamic context at the time
> document processing starts:
>
> http://www.w3.org/TR/2007/REC-xquery-20070123/#eval_context
>
> Looking at the list of components here:
>
> http://www.w3.org/TR/2007/REC-xquery-20070123/#id-xq-evaluation-context-components
>
> ... you can see that the current node, and therefore the tree of the
> current node, can be overwritten by the implementation.
>
> With Saxon, that current node and tree depends on the presence of the
> command line argument.
>
> I cannot quickly find the MarkLogic documentation of the value of the
> current node in the dynamic context at the start of
> processing. Perhaps someone else can post this.
>
> > From a stach trace it looks like fn:collection() is inserted behind
> > the scenes. If so, what's the thinking for that doing that?
>
> The static context defines what fn:collection() returns:
>
> http://www.w3.org/TR/2007/REC-xquery-20070123/#static_context
> http://www.w3.org/TR/2007/REC-xquery-20070123/#id-xq-static-context-components
> "Statically-known collections" - augmentable
>
> And so, again, it is up to the processor to define this. I believe
> MarkLogic returns the set of document nodes of every document.
>
> >Also, it looks like this is a way to be able to write somewhat
> >dynamic XQuery by doing something like this:
> >
> >let $query := /article/title
> >
> >return fn:collection()/$query
> >
> >Which apparently is the equivalent of fn:collection()//article/title
>
> That is not valid XPath 2.0 or XQuery 1.0 syntax.
>
> I hope this helps.
>
> . . . . . . . . . Ken
>
> --
> Contact us for world-wide XML consulting & instructor-led training
> Crane Softwrights Ltd. http://www.CraneSoftwrights.com/q/
> G. Ken Holman mailto:[email protected]
> Legal business disclaimers: http://www.CraneSoftwrights.com/legal
>
> _______________________________________________
> General mailing list
> [email protected]<mailto:[email protected]>
> http://developer.marklogic.com/mailman/listinfo/general
> _______________________________________________
> General mailing list
> [email protected]<mailto:[email protected]>
> http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general