Re: Levenshtein distance in XSLT 2.0

I implemented an XQuery version of the naive algorithm a little while back for a user on our forums:

http://forums.oracle.com/forums/thread.jspa?messageID=1570246&#1570246

declare function local:lev($arg1 as xs:string, $arg2 as xs:string) as xs:decimal?
{
  if(string-length($arg1) = 0) then string-length($arg2) else
  if(string-length($arg2) = 0) then string-length($arg1) else
  min((
    local:lev(substring($arg1, 2), $arg2) + 1,
    local:lev($arg1, substring($arg2, 2)) + 1,
    local:lev(substring($arg1, 2), substring($arg2, 2)) +
      (if(substring($arg1, 1, 1) = substring($arg2, 1, 1)) then 0 else 1)
  ))
};

Personally I think I find it easier to read with a non-XML syntax - but maybe I just haven't spent long enough looking at XSLT code.

Reply

The content of this field is kept private and will not be shown publicly.