Search code examples
xquery

Calling JAVA Method from XQuery


I am trying to call a JAVA method from XQuery. But it is failing

I have declared the class like:

declare namespace b64 = "java:java.util.Base64";

And I am calling it like:

(: Encode a string into Base64 :)
declare function javautil:encodebase64($in as xs:string) as xs:string {
    b64:getEncoder().encodeToString($in)
};

(: Decode a string from Base64 :)
declare function javautil:decodebase64($in as xs:string) as xs:string {
    b64:getDecoder().decode($in)
};

But I get the error:

XPST0003: XQuery syntax error in #...64:getEncoder().encodeToString#:
expected "}", found "."

I am not sure if its a syntax problem or something else. I looked for examples on Google. But most of them are restricted to a single method call. Not chained method like getEncoder().encodeToString(). Any help would be appreciated. Thanks!


Solution

  • You're confusing Java syntax with XQuery syntax - there's no "." operator in XQuery.

    In XQuery 1.0 I would expect to see b64:decode(b64:getDecoder(), $in), or in XQuery 3.1 b64:getDecoder() => b64:decode($in)