Database Reference
In-Depth Information
//act on the invoked function name
if
(
isCalledAs
(
qnHelloWorld
.
getLocalName
()))
{
result
=
sayHelloWorld
();
}
else
{
throw
new
XPathException
(
"Unknown function call: "
+
this
.
getName
().
toString
());
}
return
result
;
}
//Constructs the in-memory XML document:
//* <hello>world</hello>
//@return The in-memory XML document
private
Sequence
sayHelloWorld
()
{
final
MemTreeBuilder
builder
=
new
MemTreeBuilder
();
builder
.
startDocument
();
builder
.
startElement
(
new
QName
(
"hello"
,
HelloModule
.
NS
,
HelloModule
.
NS_PREFIX
),
null
);
builder
.
characters
(
"world"
);
builder
.
endElement
();
builder
.
endDocument
();
return
builder
.
getDocument
();
}
}
Our class implements
org.exist.xquery.Function
by extending
BasicFunc
tion
.
We define the name of our XQuery function to be
hello-world
. You should
always define this within the namespace of the internal module.
The standard way of naming functions and variables in
XQuery is to use all lowercase letters and separate terms with a
hyphen.
Here we define the function signature of our XQuery function, which includes
the name, description (for documentation), any expected parameters, and the
return type. Our function will have the signature
h:hello-world() as
xs:string
.
The function signature includes the name of our function. We define the name as
a separate variable for the purposes or referencing it later as a constant—for
example, within the
eval
function.
Search WWH ::
Custom Search