Recursive function
File: Query.xquery declare namespace math = "http://java2s.com"; declare variable $n as xs:integer external; declare function math:factorial($integer as xs:integer) as xs:double { if ($integer gt 1) then $integer * math:factorial($integer - 1) else 1 }; concat(2, "! = ", math:factorial(2)) Output: <?xml version="1.0" encoding="UTF-8"?>2! = 2