type Doc = Text;; type Text = [ (Char | (Letter+ ' '* Note))* ];; type Letter = 'a'--'z' | 'A'--'Z';; type Note = [ PCDATA ];; type Flow = [ (Char | [ PCDATA ])* ];; type Notes = [ [ PCDATA ]* ];; type Result = [ Flow Notes ];; let fun format (s : Doc) : Result = let (body,notes) = text (s,1) in [ body notes ];; let fun text ( (Text,Int) -> (Flow,Notes) ) | ([ pre::Char*? (word::Letter+ ' '* n); rem ], count) -> let (body,notes) = text (rem, count + 1) in (pre @ [word] @ body, [n] @ notes) | (body,_) -> (body, []);; match load_xml "tests/notes.xml" with | x & Doc -> format x | _ -> raise "Invalid input document";;