XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (799 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
6.23Mb size Format: txt, pdf, ePub

Saxon looks for methods in the specified class that have the right name and the right number of arguments, and if there is more than one, it tries to find the one that is the best fit to the arguments supplied. For convenience, a hyphenated XPath name such as
get-random-number()
is mapped to the camelCased Java name
getRandomNumber()
.

The above example uses the function name
new()
to call a constructor on the specified class. It is also possible to call static methods in the same way, by using the actual method name. If you want to call an instance-level method (that is, a non-static method), you can do this by supplying the relevant object as an extra first argument, thus:


where
$today
was defined as in the previous example. Public fields in the instance can be accessed as if they were zero-argument methods.

If you have problems with Saxon being unable to locate the classes and methods you want to call, try the
-TJ
option on the command line, which gives you detailed diagnostics. Remember that Saxon will only look for classes that are on the classpath, and (a common pitfall) that the classpath is effectively empty if you invoke Java using the
-jar
option on the command line. Also remember that the class and method must both be public.

Writing Extension Functions under .NET

When you run Saxon under .NET, it is still possible to invoke external Java classes such as
java.util.Date
, provided they are in the standard system library. That's because Saxon itself relies on having the full Java library available, cross-compiled into IL. (The version currently used is the GNU Classpath library, though this may move to OpenJDK in the future.)

Loading your own Java code dynamically is a bit more tricky, and the best way is probably to use IKVMC to compile it to a .NET assembly, in which case you can load it in the same way as .NET code written in any other language.

Saxon uses the same basic technique to load a .NET assembly as it uses for Java code: the namespace URI in the function call, which should be prefixed
clitype:
, is used to identify the assembly and class to be invoked, and the local part of the function name identifies the method or property. Generally speaking, if the assembly has already been loaded by the application, then it's sufficient to supply the type name. For example, the following displays the name of the current user:


If it's necessary to load the assembly dynamically, then more information is needed. This is supplied in the form of query parameters to the URI. To load a class
Employee
from the
Payroll
assembly held in the Global Assembly Cache with a particular version number and strong name, you might use a call such as:

“clitype:Acme.Payroll.Employee?asm=payroll;version=5.0.0.1;sn=abcd0123cdef9876”/>

Collations

One of the new features in XSLT 2.0 and XPath 2.0 is that all comparison and sorting of strings can be controlled using collations. This is because the rules for sorting and comparison vary from one language (and one application) to another. Collations are identified using a URI; like namespace URIs, these are not expected to identify real resources on the Web, but simply act as globally unique identifiers.

The specifications say nothing about how collation URIs are established or what they mean, so each product has to devise its own naming scheme. This section explains how it's done in Saxon.

Both Java and .NET offer extensive support for defining collations, so the approach that Saxon adopts is to provide a parameterized URI that identifies the required properties of the collation, which are then used to obtain a collator object from the underlying platform to perform the string comparisons. The collation URI takes the general form:

http://saxon.sf.net/collation?keyword=value;keyword=value;…

The parameters you are most likely to use are
lang
, which defines the required language (for example,
lang=sv
selects Swedish),
ignore-case
(values
yes
and
no
), which defines whether the difference between upper case and lower case is significant, and
ignore-modifiers
(values
yes
and
no
), which defines whether accents should be ignored. Other options available include
case-order
(values
upper-first
and
lower-first

Other books

Crazy For You by Cheyenne McCray
My Pirate Lover by Stewart, Lexie
The Wooden Skull by Benjamin Hulme-Cross
Punishment by Holt, Anne;
Dragon's Lair by Sara Craven
A Matter of Trust by Maxine Barry
The Best of Friends by Joanna Trollope
Cold Copper Tears by Glen Cook