[xquery-talk] Get the context item form function

John Snelson john.snelson at marklogic.com
Tue Aug 16 03:57:37 PDT 2011


The context item is not available inside a function - you'll have to 
pass it in explicitly as an argument:

declare function local:main($context) as element()*{
     let $items := $context//item
     return $items
};

<result>{
local:main(.)
}</result>

John

On 16/08/11 11:41, Stefan Ivanov wrote:
> Hi
>
> I'm using saxon 9.1.0.8 and trying to implement a XQuery processor.
> I use the XQJ API.
>
> I have a simple XQuery declaring a function. The declared function is
> called from the body without any parameters as follows:
>
> -- start snippet --
> declare function local:main() as element()*{
>      let $items := //item
>      return $items
> };
>
> <result>{
> local:main()
> }</result>
> -- end snippet --
>
> Executing this from some XML tool is o.k. but from the my Java Processor
> the result ist: XPDY0002: The context item is undefined at this point
> The exception is thrown when the expression is loaded
>
> My JAVA Code look like this:
> -- start snippet --
> ...
> XQConnection conn = ds.getConnection();
> XQPreparedExpression exp = conn.prepareExpression(<the qyery as input
> stream>);
> exp.bindDocument(XQConstants.CONTEXT_ITEM, <source-document>, null);
> XQResultSequence somer = exp.executeQuery();
> ...
> -- end snippet --
>
> If I try to bind the <source-document> as external document-node()
> variable and reference this node from the function the query is
> evaluated successfully:
>
> -- start snippet --
> declare variable $somedoc as document-node() external;
>
> declare function local:main() as element()*{
>      let $item:= $somedoc//item
>      return $item
> };
> ...
> -- end snippet --
>
> I tried also to ref the context with the doc(.) function but for now I'm
> unsuccessful.
> Any help is appreciated.
>


-- 
John Snelson, Senior Engineer                  http://twitter.com/jpcs
MarkLogic Corporation                         http://www.marklogic.com


More information about the talk mailing list