1 declare function local:word-wrap(
2 $string as xs:string,
3 $width as xs:integer)
4
5 {
6 let $t := normalize-space($string)
7 return
8 if (string-length($t) < $width or string-length($t) = $width)
9 then $t
10 else
11 let $idx :=
12 if (matches($t, "\s"))
13 then functx:index-of-string-last(substring($t, $width), " ")
14 else $width
15 return
16 concat(
17 string:trim-right(substring($t, 1, $idx)),
18 out:nl(),
19 local:word-wrap(string:trim-left(substring($t, $idx)),$width)
20 )
21 };
22
23 local:word-wrap("This is a very long line of text. It goes well beyond 80 (eighty) characters in width. Therefore we need a word wrap function, which we test in here. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim ve iam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",80)