Changes

Jump to navigation Jump to search
209 bytes added ,  16:45, 22 December 2015
no edit summary
* <code>java:java.lang.String</code> → <code>java.lang.String</code>
==Functionsand Variables==
Function names are A Java variable is retrieved, and a Java function is invoked, by a usual XQuery function call. The namespace of its QName identifies the class, and the local part, which is rewritten to Java’s camel case notation, identifies a variable or function of that class:
* <code>getQ{java.lang.Integer}MIN_VALUE()</code> → <code>Integer#MIN_VALUE</code><br/> * <code>Q{java.lang.Object}hash-contentscode($x as xs:string)</code> → <code>getContentsObject#hashCode(String x)</code><br/> 
=Namespace Declarations=
Java classes can be declared via namespaces. The namespace can then be used to call static functions contained in that class. Variables are represented as function with 0 parameters. The In the following example uses , Java’s {{Code|Math}} class to return is referenced. When executed, the query returns the cosine of an angle by calling the static method {{Code|cos()}}, and the value of π by addressing the static variable via {{Code|PI()}}:
<pre class="brush:xquery">
</pre>
The new With the [[XQuery 3.0#Expanded QNames|Expanded QName]] notation of XQuery 3.0,allows you to the namespace can directly specify a namespace URI instead of be embedded in the prefixfunction call:
<pre class="brush:xquery">
</pre>
The constructor of a class can be invoked by calling the virtualfunction {{Code|new()}}. Instance methods can then called bypassing on the resulting Java object as first argument.
In the following example, 256 bytes are written to the file {{Code|output.txt}}.First, a new {{Code|FileWriter}} instance is created, and its {{Code|write()}}function is called in the next step:
<pre class="brush:xquery">declare namespace fw = "java.io.FileWriter";
</pre>
Note that Java code cannot be pre-compiled, and will often as such be evaluated slower than optimizedXQuery code.
=Module Imports=
Java code can also be integrated by ''importing'' classes as modules.A new instance of the addressed class is created, which can then be accessed in the query body.
An The following, side-effecting example returns the number of distinct values added to a hash set (the boolean values returned by {{Code|set:add()}} are ignoredwill be swallowed):
<pre class="brush:xquery">
import module namespace set = "java.util.HashSet";
let $loop prof:= void( set:addfor $s in ("checkone", "two"), set:add("whatone"), return set:add("happens"$s)),return set:size()
</pre>
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu