Extensibility


In this section I will describe the facilities Saxon provides for user -written extension functions, and also the way that Saxon handles collations. Following this, I'll look at a few of the extension functions that come ready-supplied with the Saxon product.

For all these extensions, the namespace prefix «saxon » needs to be declared as «xmlns:saxon=" http://saxon.sf.net/ " » .

Writing Extension Functions

Saxon allows you to write extension functions in Java, using mechanisms based on those that were defined in the since- abandoned XSLT 1.1 draft.

An external Java class may be defined using the <saxon: script> declaration. So, if you want to use methods in the class java.util.Date , you can define:

  <saxon:script language="java"   implements-prefix="Date"   src="java:java.util.Date"/>  

and then call a method or constructor such as

  <xsl:variable name="today" select="Date:new()"/>  

This returns an XPath value that is a wrapper for a Java object of class java.util.Date . Saxon maps Java classes into the XPath type hierarchy (as a new kind of atomic value), so you can declare the type of this value as:

  <xsl:variable name="today" select="Date:new()"   as="class:java.util.Date"   xmlns:class="http://saxon.sf.net/java-type"    />   

There is also a short-cut way of binding Java methods. If the namespace prefix Date is bound to a namespace URI such as java:java.util.Date , then the <saxon: script> element above is implicit. This means that you can make a call such as:

  <xsl:variable name="today"   select="Date:new() "   xmlns:Date="java:java.util.Date"   as="class:java.util.Date"   xmlns:class="http://saxon.sf.net/java-type"/>  

with no further declaration of the external class.

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 () .

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.

Java offers extensive support for defining collations, so the approach that Saxon adopts is to provide a parameterized URI that identifies an appropriately configured instance of class «java.lang. Collator » , which is then used 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), and strength , which defines how sensitive the collation is to minor variations between characters . The four strengths are «primary » , «secondary » , « tertiary » , and «identical » . The difference between two different letters , such as «A » and «B » , is considered a primary difference; upper case versus lower case is considered a secondary difference; and accents and other diacriticals represent tertiary differences. So a collation with «strength=primary » will ignore both case and accents, while «strength=secondary » will ignore accents but not case. Generally if you are matching words in natural language text, you should use a low-strength collation. But for sorting, a high-strength collation is appropriate: This will ensure that words that differ only in their accents are sorted in the correct way, even though they might compare equal in a search.

For other parameters that you can include in a Saxon collation URI, see the product documentation. If you want the ultimate in control, the collation URI can identify a user-written implementation of the «java.lang.Collator » interface.

Because collation URIs are unlikely to be portable across implementations , it's a good idea to define them as stylesheet parameters. For example, you can define a stylesheet parameter:

  <xsl:param name="sorting-collation"   select="'http://saxon.sf.net/collation?lang=de;strength=tertiary "/>  

and then use this in a sort , by specifying:

  <xsl:sort select="value" collation="{$sorting-collation}"/>  

You could also define the collation using a conditional expression, using the system-property () function to determine which vendor's XSLT processor is currently in use.




XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net