Tail () function in XQuery

Is there a way in XQuery to do something like a function tail()?

What I'm trying to do is get the contents of the file (using " xdmp: fileystem-file ($ path) ") and then only display the last 100 lines. I can't seem to find a good way to do this. Any ideas?

Thank.

+3
source share
3 answers

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)
};
+4
source

XPath 2.0 - XQuery, XSLT PL XPath 2.0:

for $numLines in count(tokenize(., '
'))
  return
    tokenize(., '
')[position() gt $numLines -100]

for $numLines in count(tokenize(., '
'))
 return
    subsequence(tokenize(., '
'), $numLines -100 +1)
+1

xdmp: file-xx , -

let $f := 'any file system path'
return fn:tokenize(xdmp:filesystem-file($f), '[\n\r]+')[ fn:last() - 2 to fn:last()]

here I used a new line and carriage return as a token separator. if you need something else so tokenize u can. but a simple tail of the log file, then this solution works fine.

This example returns the last 2 lines of the given file. if you want more than change fn: last () - from 2 to fn: last () - x

0
source

All Articles