FSharp.Data.Xsd


Sequence and Choice

A sequence is the most common way of structuring elements in a schema.

The following xsd defines foo as a sequence made of an arbitrary number of bar elements followed by a single baz element.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
    type T = XmlProvider<Schema = """
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
          elementFormDefault="qualified" attributeFormDefault="unqualified">
            <xs:element name="foo">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="bar" type="xs:int" maxOccurs="unbounded" />
                  <xs:element name="baz" type="xs:date" minOccurs="1" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
        </xs:schema>""">

here a valid xml element is parsed as an instance of the provided type, with two properties corresponding to barand baz elements, where the former is an array in order to hold multiple elements:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
    let foo = T.Parse """
    <foo>
        <bar>42</bar>
        <bar>43</bar>
        <baz>1957-08-13</baz>
    </foo>"""

    assert(foo.Bars.[0] = 42)
    assert(foo.Bars.[1] = 43)
    assert(foo.Baz = System.DateTime(1957, 08, 13))

Instead of a sequence we may have a choice:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
    type T = XmlProvider<Schema = """
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
          elementFormDefault="qualified" attributeFormDefault="unqualified">
            <xs:element name="foo">
              <xs:complexType>
                <xs:choice>
                  <xs:element name="bar" type="xs:int" maxOccurs="unbounded" />
                  <xs:element name="baz" type="xs:date" minOccurs="1" />
                </xs:choice>
              </xs:complexType>
            </xs:element>
        </xs:schema>""">

although a choice is akin to a union type in F#, the provided type still has properties for bar and baz directly available on the foo object; in fact the properties representing alternatives in a choice are simply made optional (notice that for arrays this is not even necessary because an array can be empty). This decision is due to technical limitations (discriminated unions are not supported in type providers) but also preferred by the authors because it improves discoverability: intellisense can show both alternatives. There is a lack of precision but this is not the main goal.

1: 
2: 
3: 
4: 
5: 
6: 
    let foo = T.Parse """
    <foo>
      <baz>1957-08-13</baz>
    </foo>"""
    assert(foo.Bars.Length = 0)
    assert(foo.Baz = Some (System.DateTime(1957, 08, 13)))

Another xsd construct to model the content of an element is all, which is used less often and it's like a sequence where the order of elements does not matter. The corresponding provided type in fact is essentially the same as for a sequence.

Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
Multiple items
namespace FSharp.Data

--------------------
namespace Microsoft.FSharp.Data
type T = XmlProvider<...>

Full name: Sequences.Sequences.T
type XmlProvider

Full name: FSharp.Data.XmlProvider


<summary>Typed representation of a XML file.</summary>
       <param name='Sample'>Location of a XML sample file or a string containing a sample XML document.</param>
       <param name='SampleIsList'>If true, the children of the root in the sample document represent individual samples for the inference.</param>
       <param name='Global'>If true, the inference unifies all XML elements with the same name.</param>
       <param name='Culture'>The culture used for parsing numbers and dates. Defaults to the invariant culture.</param>
       <param name='Encoding'>The encoding used to read the sample. You can specify either the character set name or the codepage number. Defaults to UTF8 for files, and to ISO-8859-1 the for HTTP requests, unless `charset` is specified in the `Content-Type` response header.</param>
       <param name='ResolutionFolder'>A directory that is used when resolving relative file references (at design time and in hosted execution).</param>
       <param name='EmbeddedResource'>When specified, the type provider first attempts to load the sample from the specified resource
          (e.g. 'MyCompany.MyAssembly, resource_name.xml'). This is useful when exposing types generated by the type provider.</param>
       <param name='InferTypesFromValues'>If true, turns on additional type inference from values.
          (e.g. type inference infers string values such as "123" as ints and values constrained to 0 and 1 as booleans. The XmlProvider also infers string values as JSON.)</param>
       <param name='Schema'>Location of a schema file or a string containing xsd.</param>
val foo : XmlProvider<...>.Foo

Full name: Sequences.Sequences.foo
XmlProvider<...>.Parse(text: string) : XmlProvider<...>.Foo


Parses the specified XML string
property XmlProvider<...>.Foo.Bars: int []
property XmlProvider<...>.Foo.Baz: System.DateTime
namespace System
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
System.DateTime()
   (+0 other overloads)
System.DateTime(ticks: int64) : unit
   (+0 other overloads)
System.DateTime(ticks: int64, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
module Choices

from Sequences
type T = XmlProvider<...>

Full name: Sequences.Choices.T
val foo : XmlProvider<...>.Foo

Full name: Sequences.Choices.foo
property System.Array.Length: int
property XmlProvider<...>.Foo.Baz: Option<System.DateTime>
union case Option.Some: Value: 'T -> Option<'T>
Fork me on GitHub