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

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

Calling Extension Functions

Extension functions are always called from within an XPath expression. A typical function call looks like this:

my:function($arg1, 23, string(title))

The name of an extension function will always contain a namespace prefix and a colon. The prefix (
my
in this example) must be declared in a namespace declaration on some containing element in the stylesheet, in the usual way. The function may take any number of arguments (zero or more), and the parentheses are needed even if there are no arguments. The arguments can be any XPath expressions; in our example, the first argument is a variable reference, the second is a number, and the third is a function call. The arguments are passed to the function by value, which means that the function can never modify the values of the arguments (though if you pass nodes, the function may be able to modify the contents of the nodes). The function always returns a result.

We'll have more to say about the types of the arguments, and the type of the result, in due course.

What Language Is Best?

Many processors offer only one language for writing extension functions (if indeed they allow extension functions at all), so the choice may already be made for you. Some processors offer a choice; for example Xalan-J supports both Java and JavaScript. Microsoft's MSXML supports any of the usual scripting languages, for example JScript and VBScript, and the
System.Xml.Xsl
processor in .NET allows any .NET language (C#, C++, ASP.NET, VB.NET, and so on).

Generally, I'd suggest using the native language for your chosen processor; for example, Java for Oracle, Saxon on Java, and Xalan-J; JScript for MSXML; Python for 4XSLT; Eiffel for Gestalt; and C# for Saxon on .NET. If you want to use your stylesheet with more than one processor, write one version of the extension function for each language.

Client-Side Script

If you are generating HTML pages, your stylesheet can put anything it likes in the HTML page that it is generating. This includes