Database Reference
In-Depth Information
final SequenceIterator itVisitors = args [ 2 ]. iterate ();
while ( itVisitors . hasNext ()) {
final String visitor = itVisitors . nextItem (). getStringValue ();
visitors . add ( visitor );
}
result = sayHello ( greeter , greeting , visitors );
} else {
throw new XPathException ( "Unknown function call: " +
this . getName (). toString ());
}
return result ;
}
/**
* Says a greeting to many people
*
* @param greeter The name of the person saying the greeting
* @param greeting The greeting to use
* @param visitors The visitors to say the greeting to
*
* @return A sequence of greetings, one for each visitor
*/
private Sequence sayHello ( final String greeter , final String greeting ,
final List < String > visitors ) throws XPathException {
final Sequence results = new ValueSequence ();
for ( final String visitor : visitors ) {
final StringValue result =
new StringValue ( greeter + " says " + greeting + " to " + visitor );
results . add ( result );
}
return results ;
}
We add a switch on the name of our new function.
From the first Sequence in the array, we extract the first item and get its string
value. This is the value of our greeter parameter. Note that while indexes in
sequences in XQuery start at 1 , in Java they start at 0 .
As our second parameter, greeting , is optional, we first check whether an
xs:string value or an empty sequence was given.
If a value for the greeting parameter was given, we extract it.
Search WWH ::




Custom Search