Extending the Saxon Processor

Michael Kay’s excellent Saxon processor also provides an extension mechanism. One of the nice features of Saxon’s extensibility mechanism is that you can implement your own sort functions. When we discussed the <xsl:sort> element a couple of chapters ago, we mentioned that it has a lang attribute that defines the language of the things being sorted. While Xalan doesn’t currently support this attribute (although by the time you’re reading this, it might), Saxon lets you create your own extension function to handle the sorting. Your extension function must extend the com.icl.saxon.sort.TextComparer class. Here’s a sample XML document we’ll use to illustrate this function:

<?xml version="1.0"?>
<wordlist>
  <word>campo</word>
  <word>luna</word>
  <word>ciudad</word>
  <word>llaves</word>
  <word>chihuahua</word>
  <word>arroz</word>
  <word>limonada</word>
</wordlist>

This document contains Spanish words that are sorted differently than they would be in English. (In Spanish, “ch” and “ll” are separate letters that sort after “c” and “l,” respectively.) We’ll write a stylesheet that uses three <xsl:template>s to illustrate how our extension function works. Here’s the stylesheet:

<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" indent="no"/> <xsl:strip-space elements="*"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="$newline"/> ...

Get XSLT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.