Skip to main content
edited tags; edited title
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Who Transforming XML to eliminate a pyramide of OptionHTML with XSLT

Source Link

Who to eliminate a pyramide of Option

I'm still learning from designing with types non strings by Scott Wlaschin. For my project I transform some XML to HTML with xslt. Because every filename can have side effects, they are stored in string option types. If I do the transformation I get an ugly pyramid of Option.map and five times more lines of code compared to the simple version. Is there a shorter way to write that code?

open System.Xml.Xsl type HtmlPage = HtmlPage of string type XmlFile = XmlFile of string type XslFile = XslFile of string type HtmlTransformer = { HtmlPage : HtmlPage option XmlFile : XmlFile option XslFile : XslFile option } let toHtmlNoOption (xmlFn:string) (xsltFn:string) (htmlFn:string) = try let xslt = new XslCompiledTransform() xslt.Load xsltFn xslt.Transform (xmlFn,htmlFn) with _-> () let toHtml (sc:HtmlTransformer) = try let xslt = new XslCompiledTransform() let xfn = sc.XslFile |> Option.map (fun (XslFile xf) -> xf) let xmlFn = sc.XmlFile |> Option.map (fun (XmlFile xf) -> xf) let htmlFn = sc.HtmlPage |> Option.map (fun (HtmlPage lp) -> lp) xfn |> Option.map xslt.Load |> Option.map (fun () -> htmlFn |> Option.map (fun hFn -> xmlFn |> Option.map (fun xFn -> xslt.Transform(xFn,hFn))) ) with _ -> None