basex 7.0.2: no context item for root error

Saturday, 10 December 2011 · 4 messages · last reply 11 Dec 2011
AB
Andy Bunce10 Dec 2011 23:55 #
Hi with the factbook.xml database open in the 7.0.2 gui
declare function local:linkto($id as xs:string){
  let $d:=//city
  let $a:=$d[@id=$id]
  return $a
};
local:linkto("f0_1461")
Gives the "no context item for root" error on the //city line, but
declare function local:linkto($id as xs:string){
  let $a:=//city[@id=$id]
  return $a
};
local:linkto("f0_1461")
Works fine. Should it give the same error?
If it should, and I try to pass the nodes into the function then the
relative performance is not good at all.
declare function local:linkto2($id as xs:string,$nodes as node()*){
 let $c:=$nodes[@id=$id]
 let $n:=name($c[1])
 return <a href="/fb/{$n}/{$id}">
 {$c/name/string()}
 </a>
};

let $caps:=//country/@capital/string()
for $id in $caps
return local:linkto2($id,//city)
/Andy
CG
Christian Grün11 Dec 2011 00:02 #
Hi Andy, yes, it should give the same error. I only have tried the latest 7.1 snapshot, which returns errors for both of the queries. The performance may be suboptimal here, as the equality predicate in the function cannot be optimized for index access (this is because the incoming nodes may relate to a database instance that has no attribute index). This may be changed in future by choosing the best evaluation plan at runtime. Hope this helps, Christian
Quoted text · Andy Bunce · 35 lines
On Sat, Dec 10, 2011 at 11:55 PM, Andy Bunce <bunce.andy at gmail.com> wrote: > Hi > with the factbook.xml database open in the 7.0.2 gui > > declare function local:linkto($id as xs:string){ > let $d:=//city > let $a:=$d[@id=$id] > return $a > }; > local:linkto("f0_1461") > > Gives the "no context item for root" error on the //city line, but > > declare function local:linkto($id as xs:string){ > let $a:=//city[@id=$id] > return $a > }; > local:linkto("f0_1461") > > Works fine. Should it give the same error? > If it should, and I try to pass the nodes into the function then the > relative performance is not good at all. > declare function local:linkto2($id as xs:string,$nodes as node()*){ > let $c:=$nodes[@id=$id] > let $n:=name($c[1]) > return <a href="/fb/{$n}/{$id}"> > {$c/name/string()} > </a> > }; > > let $caps:=//country/@capital/string() > for $id in $caps > return local:linkto2($id,//city) > > /Andy
AB
Andy Bunce11 Dec 2011 00:15 #
Hi Christian, Thanks for the explanation. So functions never use indices are the moment? /Andy On Sat, Dec 10, 2011 at 11:02 PM, Christian Grün <christian.gruen at gmail.com>wrote:
Quoted text · Christian Grün · 50 lines
> Hi Andy, > > yes, it should give the same error. I only have tried the latest 7.1 > snapshot, which returns errors for both of the queries. The > performance may be suboptimal here, as the equality predicate in the > function cannot be optimized for index access (this is because the > incoming nodes may relate to a database instance that has no attribute > index). This may be changed in future by choosing the best evaluation > plan at runtime. > > Hope this helps, > Christian > > > On Sat, Dec 10, 2011 at 11:55 PM, Andy Bunce <bunce.andy at gmail.com> wrote: > > Hi > > with the factbook.xml database open in the 7.0.2 gui > > > > declare function local:linkto($id as xs:string){ > > let $d:=//city > > let $a:=$d[@id=$id] > > return $a > > }; > > local:linkto("f0_1461") > > > > Gives the "no context item for root" error on the //city line, but > > > > declare function local:linkto($id as xs:string){ > > let $a:=//city[@id=$id] > > return $a > > }; > > local:linkto("f0_1461") > > > > Works fine. Should it give the same error? > > If it should, and I try to pass the nodes into the function then the > > relative performance is not good at all. > > declare function local:linkto2($id as xs:string,$nodes as node()*){ > > let $c:=$nodes[@id=$id] > > let $n:=name($c[1]) > > return <a href="/fb/{$n}/{$id}"> > > {$c/name/string()} > > </a> > > }; > > > > let $caps:=//country/@capital/string() > > for $id in $caps > > return local:linkto2($id,//city) > > > > /Andy >
CG
Christian Grün11 Dec 2011 01:56 #
Quoted text · Andy Bunce · 1 line
> Thanks for the explanation. So functions never use indices are the moment?
..unless the documents/database are specified within the function. For example, the following function will indeed be optimized, if the 'DB' database provides a text index:
  declare function($key as xs:string) {
    db:open('DB')//*[text() = $key]
  };
However, you can explicitly take advantage of a text index by using the db:text() function:
  declare function($db, $key) {
    db:text($db, $key)/....
  };
Best, Christian

⚡Generated with XQuery