Project

Profile

Help

XPDY0002: Leading '/' cannot select the root node of the tree containing the context item

Added by Paul BEDE almost 8 years ago

+CODE :+

// the Saxon processor object Processor saxon = new Processor(false);

// parse the string as a document node DocumentBuilder build = saxon.newDocumentBuilder(); String input = FileUtils.readFileToString(new File("myFile.XML"), "UTF-8"); Source src = new StreamSource(new StringReader(input)); XdmNode document = build.build(src);

// compile the query XQueryCompiler compiler = saxon.newXQueryCompiler(); XQueryExecutable exec = compiler.compile(new File("myQuery.xq"));

+ERROR :+ Error on line 775 of myQuery.xq: XPDY0002: Leading '/' cannot select the root node of the tree containing the context item: Exception in thread "main" net.sf.saxon.s9api.SaxonApiException: Leading '/' cannot select the root node of the tree containing the context item: the context item is undefined at net.sf.saxon.s9api.XQueryCompiler.compile(XQueryCompiler.java:482) at fr.test.essai.main(essai.java:123)

+FILE XQUERY :+

declare function local:checkRefTest() as xs:string { (:text and explication here. :) let $REFTEST := /dmodule/descendant::REFTEST (=> line 482 HERE) return let $refTestKO := for $item in $REFTEST return if(fn:string-length($item/@RefTest)= 13) then let $RefTest := fn:substring($item/@RefTest, 1, 6) return if ($RefTest = " ") then if(fn:exists($item/@refval) and fn:exists($item/@refval2)) then () else () else () else $item return if(fn:empty($refTestKO)) then "OK" else fn:concat("NOK : test here ", "'",fn:string-join($refTestKO/@RefTest, "', '"),"'") };

+QUESTION :+

  • How do i solve this error ? if I can't modify XQuery file because it's come from a industrial company
  • How indicate context item in this case ? or other...

Please help me


Replies (1)

RE: XPDY0002: Leading '/' cannot select the root node of the tree containing the context item - Added by Michael Kay almost 8 years ago

I'm afraid you're going to have to fix the query because it's incorrect. Within a function, there is no context item, therefore you can't use a path that depends on the context item.

The simplest fix might be to bind a global variable to the context item, and then select relative to that:

declare variable $input := .;
declare function local:checkRefTest() as xs:string {
let $REFTEST := $input/dmodule/descendant::REFTEST

Sadly, the fact that the code came from an "industrial company" is no guarantee of its quality.

    (1-1/1)

    Please register to reply