-2

I'm trying to use the xQuery API for java s9api, however when I try to declare a namespace and run a simple rule to test it I restore the error "XPST0003: XQuery syntax error in

# ... if (funcJavaDict: CheckString #
Unexpected token "if (" beyond end of query "

Acretido is an easy error to solve, but I'm not getting to the solution,

The Java code snippet is:

//Declara namespace de funções java para usar nas regras
comp.declareNamespace ("funcJavaDict", "java:Rastreamento.retratos.DictionaryTerms");
comp.declareNamespace ("xmi", "http://www.omg.org/XMI");

//compila regra do arquivo
XQueryExecutable exp = null;
try {
    exp = comp.compile("return  /n"+
                       "if ( funcJavaDict:CheckString("em andamento","EM Andamento") ) then /n" +
                       "  String("são iguais") /n"
                       "else  /n"+
                       "  String("são diferente") ");
}catch (SaxonApiException e) {
    e.printStackTrace();
}

//carrega e executa regra
XQueryEvaluator Eval = exp.load();

XdmValue rs = null;
try {
    rs = Eval.evaluate();
} catch (SaxonApiException e) {
    e.printStackTrace();
}

the code is very simple is just to check if two string's are equivalent.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

1

Firstly, I'm puzzled by your Java snippet because it won't compile (because of the nested quotes), so I don't see how you get as far as Saxon reporting an XQuery syntax error. Also I imagine that the '/n' should really be '\n'. So let's suppose the Java actually says:

comp.compile("return \n"+
    "if ( funcJavaDict:CheckString('em andamento','EM Andamento') ) then \n" +
    "  String('são iguais') \n"
    "else  \n"+
   "  String('são diferente') ");

XQuery keywords are recognized only if they appear in the right context. "return" at the start of an expression is not recognized as a keyword, so it is interpreted as a path expression meaning child::element(return).

(In fact recent Saxon releases will often report a warning if you use a keyword incorrectly like this, but it's not catching this particular case).

So "return" is a complete expression that selects child elements named return, and the only thing that can come after a complete expression is either the end of the input, or an infix operator such as and or union. The keyword if is not either of these things, so the compiler reports a syntax error. The solution is to remove the return keyword.

But that's not the only thing wrong with your query. There is no function named String. You could change it to string and the query would work, but writing string('xyz') is just a long-winded way of writing 'xyz', so better to drop the function call entirely.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • This error was because I re-wrote without much attention but in the return of the string 'alike' or 'different' string still gives syntax error, the code is this way (the part where the xQuery code is): comp.compile( "if ( funcJavaDict:CheckString('em andamento','EM Andamento') ) then \n" + " return 'são iguais' \n" "else \n"+ " return 'são diferente' "); – Artulanez Souza Sep 12 '18 at 23:47
  • Would you have any other suggestions? – Artulanez Souza Sep 12 '18 at 23:47
  • If you want help understanding an error message then it helps to tell us what the error message is. I suspect it's telling you that you don't need the "return" keyword here. – Michael Kay Sep 13 '18 at 08:15