JACK M
JACK M

Reputation: 2841

XQuery: using global var in function

I need to use a counter to remember how many node I have dealed with. So I defined a global var $classCounter. For some unknown reasons, I get an error from zorba:

test.xqy>:15,9: error [zerr:XSST0004]: "local:owlClassNameBuilerHelper": function declared nonsequential but has sequential body

I really don't understand what this error means. How to implement a global counter in XQuery?

The whole xqy file is:

declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace owl="http://www.w3.org/2002/07/owl#";
declare namespace xsd="http://www.w3.org/2001/XMLSchema#";
declare namespace rdfs="http://www.w3.org/2000/01/rdf-schema#";

import module namespace functx="http://www.functx.com";

declare variable $srcDoc:="test_xsd.xml"; (:need to adjust the input XSD file here:)
declare variable $defaultXMLNS:="http://www.test.com#";
declare variable $defaultXMLBase:=$defaultXMLNS;


declare variable $classCounter:=0;

declare function local:owlClassNameBuilerHelper($pnode as node()*)
as xs:string?
{
  $classCounter:=classCounter+1;
  let $tmp:=""
  return
  (
    "haha"
    (:if(functx:if-empty($pnode/@name, "-1")!="-1") (:if the name attr doesn't exist:)
    then data($pnode/ancestor::element[1]/@name) (:get the name attr of first ancestor named element:)
    else data($pnode/@name):)
  )
};

element rdf:RDF
{
  namespace {""} {$defaultXMLNS},
  namespace {"owl"} {"http://www.w3.org/2002/07/owl#"},
  namespace {"xsd"} {"http://www.w3.org/2001/XMLSchema#"},
  namespace {"rdfs"} {"http://www.w3.org/2000/01/rdf-schema#"},
  attribute xml:base {$defaultXMLBase}

}

command line:

zorba -i -f -q test.xqy

Upvotes: 0

Views: 415

Answers (1)

Michael Kay
Michael Kay

Reputation: 163418

I need to use a counter to remember how many node I have dealed with.

Firstly, XQuery is a functional programming language. That's a completely different processing model: you can't "remember" what you have "dealt with", because there is no memory and no time dimension. Functions are mathematical functions, they can't have side-effects like updating global variables.

Now, the error message suggests to me that the particular XQuery processor you are using (Zorba) has extensions that allow you to depart from the pure functional programming model; but you are using the extensions incorrectly. In particular, if you want a function to have side-effects then you must declare the function as such. You'll have to look in the Zorba documentation for how to do that, because there is no standard.

Upvotes: 0

Related Questions