declare namespace xsd = "http://www.w3.org/2001/XMLSchema"; declare namespace math = "http://www.w3.org/2005/xpath-functions/math"; declare function local:haversine($lat1 as xsd:float, $lon1 as xsd:float, $lat2 as xsd:float, $lon2 as xsd:float) as xsd:float { let $dlat := ($lat2 - $lat1) * math:pi() div 180 let $dlon := ($lon2 - $lon1) * math:pi() div 180 let $rlat1 := $lat1 * math:pi() div 180 let $rlat2 := $lat2 * math:pi() div 180 let $a := math:sin($dlat div 2) * math:sin($dlat div 2) + math:sin($dlon div 2) * math:sin($dlon div 2) * math:cos($rlat1) * math:cos($rlat2) let $c := 2 * math:atan2(math:sqrt($a), math:sqrt(1-$a)) return xsd:float($c * 6371.0) }; local:haversine(36.12, -86.67, 33.94, -118.4)