In simple XQuery, this can be achieved by breaking into lines and getting the desired number of lines from the end of the sequence, and then, if necessary, joining them, i.e.
declare function local:tail($content as xs:string, $number as xs:integer)
{
let $linefeed := " "
let $lines := tokenize($content, $linefeed)
let $tail := $lines[position() > last() - $number]
return string-join($tail, $linefeed)
};
source
share