Using a Custom Collation for Comparing Text

The other way of using custom collations is in comparing text. Obviously comparing strings is part of sorting, but the new compare() function in XQuery 1.0 and XPath 2.0 lets you compare two strings. To use an example from the XSLT 2.0 spec, the German word for street can be spelled Straße or Strasse. A simple character comparison defines sees these as two different strings; we’ll use a collation function that recognizes the two words as being equal.

Here’s the complete code:

/**
 * GermanCollation.java
 */

package com.oreilly.xslt;

import java.text.ParseException;
import java.text.RuleBasedCollator;

public class GermanCollation extends RuleBasedCollator
{
  public GermanCollation() throws ParseException
  {
    super(traditionalGermanRules);
  }
  
  private static String sharpS  = new String("\u00DF");
  private static String uppercaseUmlautA = new String("\u00C4");
  private static String lowercaseUmlautA = new String("\u00E4");
  private static String uppercaseUmlautO = new String("\u00D6");
  private static String lowercaseUmlautO = new String("\u00F6");
  private static String uppercaseUmlautU = new String("\u00DC");
  private static String lowercaseUmlautU = new String("\u00FC");

  private static String traditionalGermanRules =
    ("< a,A " + 
     "<" + lowercaseUmlautA + "=ae " +
     "<" + uppercaseUmlautA + "=AE " +
     "< b,B < c,C < d,D < e,E < f,F " +
     "< g,G < h,H < i,I < j,J < k,K " +
     "< l,L < m,M < n,N < o,O " + 
     "<" + lowercaseUmlautO + "=oe " +
 "<" + uppercaseUmlautO ...

Get XSLT, 2nd Edition 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.