XQuery:在函数中使用全局变量

时间:2016-03-21 08:28:45

标签: xquery zorba

我需要使用计数器来记住我已经处理了多少个节点。所以我定义了一个全局var $ classCounter。由于某些未知原因,我从zorba收到错误:

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

我真的不明白这个错误意味着什么。如何在XQuery中实现全局计数器?

整个xqy文件是:

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}

}

命令行:

zorba -i -f -q test.xqy

1 个答案:

答案 0 :(得分:0)

  

我需要使用计数器来记住我已经处理了多少个节点。

首先,XQuery是一种函数式编程语言。这是一个完全不同的处理模式:你不能记住"你有什么"处理",因为没有记忆,也没有时间维度。函数是数学函数,它们不具有更新全局变量等副作用。

现在,错误消息告诉我您正在使用的特定XQuery处理器(Zorba)具有允许您脱离纯函数式编程模型的扩展;但是您使用的扩展程序不正确。特别是,如果您希望函数具有副作用,则必须声明该函数。您必须查看Zorba文档中的相关信息,因为没有标准。

相关问题