Light XML DSL

A light domain specific language for declaring xml in F# as code.

Copy Source
Copy Link
Tools:
 1: open System
 2: 
 3: // The DSL
 4: module LightDsl =
 5: (Structural types omitted)
 6:     let name s = Name (s)
 7:     let qname ns s = QualifiedName (ns, s)
 8: 
 9:     let (@=) name value = { Name=name; Value=value }
10:     let elem name = { Name=name; Attributes=[]; Content=Empty }
11:     let attribs a (el:XmlElement) = { el with Attributes=a }
12:     let value s (el:XmlElement) = { el with Content=Value (s) }
13:     let content items (el:XmlElement) = { el with Content=Content (items) }
14: 
15: 
16: open System.Xml.Linq
17: open LightDsl
18: 
19: [<AutoOpen>]
20: module XElementExtension =
21: (Private members omitted)
22:     module XElement =
23:         let ofLightDsl (xe:XmlElement) = map xe
24: 
25: // Usage
26: let xml = 
27:     elem (qname "http://myschema" "root")
28:     |> content [
29:         elem (name "Person")
30:         |> attribs [name "id" @= "js1"]
31:         |> content [elem (name "FullName") |> value "John Smith" ]]
32:     |> XElement.ofLightDsl
33: 
namespace System
type XmlMarkup =
    | Element of XmlElement
    | Attribute of XmlAttribute

and XmlName =
    | Name of string
    | QualifiedName of string * string

and XmlElement =
    { Name:XmlName
        Attributes:XmlAttribute list
        Content:XmlElementContent }

and XmlElementContent =
    | Empty
    | Value of string
    | Content of XmlElement list
            
and XmlAttribute =
    { Name:XmlName
        Value:String }
val name : string -> XmlName

Full name: Snippet.LightDsl.name
val s : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
union case XmlName.Name: string -> XmlName
val qname : string -> string -> XmlName

Full name: Snippet.LightDsl.qname
val ns : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
union case XmlName.QualifiedName: string * string -> XmlName
val name : XmlName

  type: XmlName
  implements: IEquatable<XmlName>
  implements: Collections.IStructuralEquatable
  implements: IComparable<XmlName>
  implements: IComparable
  implements: Collections.IStructuralComparable
val value : String

  type: String
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
union case XmlElementContent.Value: string -> XmlElementContent
val elem : XmlName -> XmlElement

Full name: Snippet.LightDsl.elem
union case XmlElementContent.Content: XmlElement list -> XmlElementContent
union case XmlElementContent.Empty: XmlElementContent
val attribs : XmlAttribute list -> XmlElement -> XmlElement

Full name: Snippet.LightDsl.attribs
val a : XmlAttribute list

  type: XmlAttribute list
  implements: Collections.IStructuralEquatable
  implements: IComparable<List<XmlAttribute>>
  implements: IComparable
  implements: Collections.IStructuralComparable
  implements: Collections.Generic.IEnumerable<XmlAttribute>
  implements: Collections.IEnumerable
val el : XmlElement

  type: XmlElement
  implements: IEquatable<XmlElement>
  implements: Collections.IStructuralEquatable
  implements: IComparable<XmlElement>
  implements: IComparable
  implements: Collections.IStructuralComparable
type XmlElement =
  {Name: XmlName;
   Attributes: XmlAttribute list;
   Content: XmlElementContent;}

Full name: Snippet.LightDsl.XmlElement

  type: XmlElement
  implements: IEquatable<XmlElement>
  implements: Collections.IStructuralEquatable
  implements: IComparable<XmlElement>
  implements: IComparable
  implements: Collections.IStructuralComparable
val value : string -> XmlElement -> XmlElement

Full name: Snippet.LightDsl.value
val content : XmlElement list -> XmlElement -> XmlElement

Full name: Snippet.LightDsl.content
val items : XmlElement list

  type: XmlElement list
  implements: Collections.IStructuralEquatable
  implements: IComparable<List<XmlElement>>
  implements: IComparable
  implements: Collections.IStructuralComparable
  implements: Collections.Generic.IEnumerable<XmlElement>
  implements: Collections.IEnumerable
namespace System.Xml
namespace System.Xml.Linq
module LightDsl

from Snippet
type AutoOpenAttribute =
  class
    inherit Attribute
    new : unit -> AutoOpenAttribute
    new : path:string -> AutoOpenAttribute
    member Path : string
  end

Full name: Microsoft.FSharp.Core.AutoOpenAttribute

  type: AutoOpenAttribute
  implements: Runtime.InteropServices._Attribute
  inherits: Attribute
let private mapName = function
    | Name n -> XName.Get (n)
    | QualifiedName (ns,n) -> XName.Get (n, ns)

let private mapAttribs (attribs:XmlAttribute list) =
    attribs |> List.map (fun a -> new XAttribute (mapName a.Name, a.Value))
                
let rec private map (e:XmlElement) =
    match e.Content with
    | Empty -> new XElement (mapName e.Name)
    | Value s ->
        let content =
            mapAttribs e.Attributes
            |> List.map (fun a -> a :> obj)
            |> List.append ([s :> obj])

        new XElement (mapName e.Name, content)
    | Content c ->
        let content =
            mapAttribs e.Attributes
            |> List.map (fun a -> a :> obj)
            |> List.append (c |> List.map (fun e -> map (e) :> obj))

        new XElement (mapName e.Name, content)
type XElement =
  class
    inherit System.Xml.Linq.XContainer
    new : System.Xml.Linq.XName -> System.Xml.Linq.XElement
    new : System.Xml.Linq.XName * obj -> System.Xml.Linq.XElement
    new : System.Xml.Linq.XName * obj [] -> System.Xml.Linq.XElement
    new : System.Xml.Linq.XElement -> System.Xml.Linq.XElement
    new : System.Xml.Linq.XStreamingElement -> System.Xml.Linq.XElement
    member AncestorsAndSelf : unit -> System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>
    member AncestorsAndSelf : System.Xml.Linq.XName -> System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>
    member Attribute : System.Xml.Linq.XName -> System.Xml.Linq.XAttribute
    member Attributes : unit -> System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute>
    member Attributes : System.Xml.Linq.XName -> System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute>
    member DescendantNodesAndSelf : unit -> System.Collections.Generic.IEnumerable<System.Xml.Linq.XNode>
    member DescendantsAndSelf : unit -> System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>
    member DescendantsAndSelf : System.Xml.Linq.XName -> System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>
    member FirstAttribute : System.Xml.Linq.XAttribute
    member GetDefaultNamespace : unit -> System.Xml.Linq.XNamespace
    member GetNamespaceOfPrefix : string -> System.Xml.Linq.XNamespace
    member GetPrefixOfNamespace : System.Xml.Linq.XNamespace -> string
    member HasAttributes : bool
    member HasElements : bool
    member IsEmpty : bool
    member LastAttribute : System.Xml.Linq.XAttribute
    member Name : System.Xml.Linq.XName with get, set
    member NodeType : System.Xml.XmlNodeType
    member RemoveAll : unit -> unit
    member RemoveAttributes : unit -> unit
    member ReplaceAll : obj -> unit
    member ReplaceAll : obj [] -> unit
    member ReplaceAttributes : obj -> unit
    member ReplaceAttributes : obj [] -> unit
    member Save : string -> unit
    member Save : System.IO.Stream -> unit
    member Save : System.IO.TextWriter -> unit
    member Save : System.Xml.XmlWriter -> unit
    member Save : string * System.Xml.Linq.SaveOptions -> unit
    member Save : System.IO.Stream * System.Xml.Linq.SaveOptions -> unit
    member Save : System.IO.TextWriter * System.Xml.Linq.SaveOptions -> unit
    member SetAttributeValue : System.Xml.Linq.XName * obj -> unit
    member SetElementValue : System.Xml.Linq.XName * obj -> unit
    member SetValue : obj -> unit
    member Value : string with get, set
    member WriteTo : System.Xml.XmlWriter -> unit
    static member EmptySequence : System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>
    static member Load : string -> System.Xml.Linq.XElement
    static member Load : System.IO.Stream -> System.Xml.Linq.XElement
    static member Load : System.IO.TextReader -> System.Xml.Linq.XElement
    static member Load : System.Xml.XmlReader -> System.Xml.Linq.XElement
    static member Load : string * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XElement
    static member Load : System.IO.Stream * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XElement
    static member Load : System.IO.TextReader * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XElement
    static member Load : System.Xml.XmlReader * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XElement
    static member Parse : string -> System.Xml.Linq.XElement
    static member Parse : string * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XElement
  end

Full name: System.Xml.Linq.XElement

  type: XElement
  implements: Xml.IXmlLineInfo
  implements: Xml.Serialization.IXmlSerializable
  inherits: XContainer
  inherits: XNode
  inherits: XObject
val ofLightDsl : XmlElement -> XElement

Full name: Snippet.XElementExtension.XElement.ofLightDsl
val xe : XmlElement

  type: XmlElement
  implements: IEquatable<XmlElement>
  implements: Collections.IStructuralEquatable
  implements: IComparable<XmlElement>
  implements: IComparable
  implements: Collections.IStructuralComparable
val private map : XmlElement -> XElement

Full name: Snippet.XElementExtension.map
val xml : XElement

Full name: Snippet.xml

  type: XElement
  implements: Xml.IXmlLineInfo
  implements: Xml.Serialization.IXmlSerializable
  inherits: XContainer
  inherits: XNode
  inherits: XObject

More information

Link: http://fssnip.net/9x
Posted: 2 years ago
Author: Huw Simpson
Tags: XML, DSL, LinqToXml