Skip to content

# Samples Modeling

The code generator supports processing xml/json documents directly. That means even without a schema you can easily create at the very least an initial draft of your models just from samples. If you use a directory with multiple samples the transformer will merge and flatten duplicate classes, fields and field types.

## XML Documents

    $ xsdata generate --package tests.fixtures.artists tests/fixtures/artists

Sample #1 Sample #2 Sample #3 Output

    <metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#">
      <artist id="1f9df192-a621-4f54-8850-2c5373b7eac9" type="Person" type-id="b6e035f4-3ce9-331c-97df-83397230b0df">
        <name>Ludwig van Beethoven</name>
        <sort-name>Beethoven, Ludwig van</sort-name>
        <disambiguation>German composer</disambiguation>
        <ipi>00002507239</ipi>
        <ipi-list>
          <ipi>00002507239</ipi>
        </ipi-list>
        <isni-list>
          <isni>0000000121268987</isni>
        </isni-list>
        <gender id="36d3d30a-839d-3eda-8cb3-29be4384e4a9">Male</gender>
        <country>DE</country>
        <area id="85752fda-13c4-31a3-bee5-0e5cb1f51dad">
          <name>Germany</name>
          <sort-name>Germany</sort-name>
          <iso-3166-1-code-list>
            <iso-3166-1-code>DE</iso-3166-1-code>
          </iso-3166-1-code-list>
        </area>
        <begin-area id="b86b7e97-c4e2-4ec2-942b-5a6cd8eea1da">
          <name>Bonn</name>
          <sort-name>Bonn</sort-name>
        </begin-area>
        <end-area id="afff1a94-a98b-4322-8874-3148139ab6da">
          <name>Wien</name>
          <sort-name>Wien</sort-name>
          <iso-3166-2-code-list>
            <iso-3166-2-code>AT-9</iso-3166-2-code>
          </iso-3166-2-code-list>
        </end-area>
        <life-span>
          <begin>1770-12-17</begin>
          <end>1827-03-26</end>
          <ended>true</ended>
        </life-span>
        <alias-list count="31">
          <alias sort-name="Beethoven" type="Artist name" type-id="894afba6-2816-3c24-8072-eadb66bd04bc">Beethoven</alias>
          <alias sort-name="Beethoven _ Ludwig Van">Beethoven _ Ludwig Van</alias>
          <alias sort-name="Beethoven, Ludwig">Beethoven, Ludwig</alias>
          <alias sort-name="Beethoven, Ludwig Van">Beethoven, Ludwig Van</alias>
          <alias sort-name="Beethoven, Ludwig Van (1770 - 1827)">Beethoven, Ludwig Van (1770 - 1827)</alias>
          <alias sort-name="Beethoven, Ludwig Von">Beethoven, Ludwig Von</alias>
          <alias sort-name="Beethoven, Ludwig van (1770-1827)">Beethoven, Ludwig van (1770-1827)</alias>
          <alias sort-name="Beethoven, Ludwig van, 1770-1827">Beethoven, Ludwig van, 1770-1827</alias>
          <alias sort-name="Bethoveen" type="Search hint" type-id="1937e404-b981-3cb7-8151-4c86ebfc8d8e">Bethoveen</alias>
          <alias sort-name="L v Beethoven">L v Beethoven</alias>
          <alias locale="lv" sort-name="Bēthovens, L. Van">L. Van Bēthovens</alias>
          <alias sort-name="Beethoven, L. Von">L. Von Beethoven</alias>
          <alias sort-name="Beethoven, L. van">L. van Beethoven</alias>
          <alias locale="en" sort-name="Beethoven, L.Van">L.Van Beethoven</alias>
          <alias sort-name="Beethoven, Ludvig Von">Ludvig Von Beethoven</alias>
          <alias sort-name="Beethoven, Ludvig van">Ludvig van Beethoven</alias>
          <alias locale="lv" sort-name="Ludvigs van Bēthovens" primary="primary">Ludvigs van Bēthovens</alias>
          <alias sort-name="Beethoven, Ludwid van">Ludwid van Beethoven</alias>
          <alias sort-name="Beethoven, Ludwig">Ludwig Beethoven</alias>
          <alias sort-name="Beethoven, Ludwig Van (1770-1827)">Ludwig Van Beethoven (1770-1827)</alias>
          <alias sort-name="Beethoven, Ludwig Von">Ludwig Von Beethoven</alias>
          <alias locale="en" sort-name="Beethoven, Ludwig van" type="Artist name" type-id="894afba6-2816-3c24-8072-eadb66bd04bc" primary="primary">Ludwig van Beethoven</alias>
          <alias sort-name="LvB" type="Search hint" type-id="1937e404-b981-3cb7-8151-4c86ebfc8d8e">LvB</alias>
          <alias sort-name="bheetoven" type="Search hint" type-id="1937e404-b981-3cb7-8151-4c86ebfc8d8e">bheetoven</alias>
          <alias sort-name="Beethoven, Ludwig van" type="Artist name" type-id="894afba6-2816-3c24-8072-eadb66bd04bc">van Beethoven, Ludwig</alias>
          <alias locale="ru" sort-name="Beethoven">Бетховен</alias>
          <alias locale="ru" sort-name="Бетховен, Л." type="Artist name" type-id="894afba6-2816-3c24-8072-eadb66bd04bc">Л. Бетховен</alias>
          <alias locale="ja" sort-name="ベートーヴェン" type="Artist name" type-id="894afba6-2816-3c24-8072-eadb66bd04bc" primary="primary">ベートーヴェン</alias>
          <alias locale="ja" sort-name="ベートーヴェン ルートヴィヒヴァン" type="Artist name" type-id="894afba6-2816-3c24-8072-eadb66bd04bc">ルートヴィヒ・ヴァン・ベートーヴェン</alias>
          <alias locale="zh_Hant" sort-name="bèi duō fēn" type="Artist name" type-id="894afba6-2816-3c24-8072-eadb66bd04bc" primary="primary">貝多芬</alias>
          <alias locale="ko" sort-name="Beethoven">베토벤</alias>
        </alias-list>
      </artist>
    </metadata>

    <metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#">
      <artist id="66c662b6-6e2f-4930-8610-912e24c63ed1" type="Group" type-id="e431f5f6-b5d2-343d-8b36-72607fffb74b">
        <name>AC/DC</name>
        <sort-name>AC/DC</sort-name>
        <disambiguation>Australian hard rock band</disambiguation>
        <isni-list>
          <isni>000000012271298X</isni>
        </isni-list>
        <country>AU</country>
        <area id="106e0bec-b638-3b37-b731-f53d507dc00e">
          <name>Australia</name>
          <sort-name>Australia</sort-name>
          <iso-3166-1-code-list>
            <iso-3166-1-code>AU</iso-3166-1-code>
          </iso-3166-1-code-list>
        </area>
        <begin-area id="3f179da4-83c6-4a28-a627-e46b4a8ff1ed">
          <name>Sydney</name>
          <sort-name>Sydney</sort-name>
        </begin-area>
        <life-span>
          <begin>1973-11</begin>
        </life-span>
        <alias-list count="7">
          <alias sort-name="AC DC">AC DC</alias>
          <alias sort-name="AC-DC">AC-DC</alias>
          <alias sort-name="AC.DC">AC.DC</alias>
          <alias sort-name="AC?DC">AC?DC</alias>
          <alias sort-name="ACDC">ACDC</alias>
          <alias sort-name="AC\DC">AC\DC</alias>
          <alias sort-name="Ballbreaker">Ballbreaker</alias>
        </alias-list>
      </artist>
    </metadata>

    <metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#">
      <artist id="b95ce3ff-3d05-4e87-9e01-c97b66af13d4" type="Person" type-id="b6e035f4-3ce9-331c-97df-83397230b0df">
        <name>Eminem</name>
        <sort-name>Eminem</sort-name>
        <ipi>00354929235</ipi>
        <ipi-list>
          <ipi>00354929235</ipi>
          <ipi>00354929431</ipi>
        </ipi-list>
        <isni-list>
          <isni>0000000117699370</isni>
          <isni>0000000368611900</isni>
        </isni-list>
        <gender id="36d3d30a-839d-3eda-8cb3-29be4384e4a9">Male</gender>
        <country>US</country>
        <area id="489ce91b-6658-3307-9877-795b68554c98">
          <name>United States</name>
          <sort-name>United States</sort-name>
          <iso-3166-1-code-list>
            <iso-3166-1-code>US</iso-3166-1-code>
          </iso-3166-1-code-list>
        </area>
        <begin-area id="bfae8151-2ba2-4f8f-bf2e-8fc16ce352c6">
          <name>St. Joseph</name>
          <sort-name>St. Joseph</sort-name>
        </begin-area>
        <life-span>
          <begin>1972-10-17</begin>
        </life-span>
        <alias-list count="13">
          <alias sort-name="EMINƎM" type="Search hint" type-id="1937e404-b981-3cb7-8151-4c86ebfc8d8e">EMINƎM</alias>
          <alias sort-name="Em" type="Artist name" type-id="894afba6-2816-3c24-8072-eadb66bd04bc">Em</alias>
          <alias locale="en" sort-name="Eminem" type="Artist name" type-id="894afba6-2816-3c24-8072-eadb66bd04bc" primary="primary">Eminem</alias>
          <alias sort-name="EminemMusic" type="Search hint" type-id="1937e404-b981-3cb7-8151-4c86ebfc8d8e">EminemMusic</alias>
          <alias sort-name="M&amp;M" type="Artist name" type-id="894afba6-2816-3c24-8072-eadb66bd04bc">M&amp;M</alias>
          <alias sort-name="M. Mathers" type="Search hint" type-id="1937e404-b981-3cb7-8151-4c86ebfc8d8e">M. Mathers</alias>
          <alias sort-name="Marshall B. Mathers III" type="Search hint" type-id="1937e404-b981-3cb7-8151-4c86ebfc8d8e">Marshall B. Mathers III</alias>
          <alias sort-name="Mathers, Marshall Bruce, III" type="Legal name" type-id="d4dcd0c0-b341-3612-a332-c0ce797b25cf">Marshall Bruce Mathers III</alias>
          <alias sort-name="Marshall Mathers III" type="Search hint" type-id="1937e404-b981-3cb7-8151-4c86ebfc8d8e">Marshall Mathers III</alias>
          <alias sort-name="Ominem">Ominem</alias>
          <alias sort-name="Slim Shady" type="Artist name" type-id="894afba6-2816-3c24-8072-eadb66bd04bc">Slim Shady</alias>
          <alias sort-name="The Real Slim Shady">The Real Slim Shady</alias>
          <alias locale="ja" sort-name="エミネム" type="Artist name" type-id="894afba6-2816-3c24-8072-eadb66bd04bc" primary="primary">エミネム</alias>
        </alias-list>
      </artist>
    </metadata>

    from __future__ import annotations

    from dataclasses import dataclass, field

    from xsdata.models.datatype import XmlDate, XmlPeriod

    __NAMESPACE__ = "http://musicbrainz.org/ns/mmd-2.0#"

    @dataclass(kw_only=True)
    class Alias:
        class Meta:
            name = "alias"
            namespace = "http://musicbrainz.org/ns/mmd-2.0#"

        locale: None | str = field(
            default=None,
            metadata={
                "type": "Attribute",
            },
        )
        sort_name: str = field(
            metadata={
                "name": "sort-name",
                "type": "Attribute",
            }
        )
        type_value: None | str = field(
            default=None,
            metadata={
                "name": "type",
                "type": "Attribute",
            },
        )
        type_id: None | str = field(
            default=None,
            metadata={
                "name": "type-id",
                "type": "Attribute",
            },
        )
        primary: None | str = field(
            default=None,
            metadata={
                "type": "Attribute",
            },
        )
        value: str = field(default="")

    @dataclass(kw_only=True)
    class BeginArea:
        class Meta:
            name = "begin-area"
            namespace = "http://musicbrainz.org/ns/mmd-2.0#"

        id: str = field(
            metadata={
                "type": "Attribute",
            }
        )
        name: str = field(
            metadata={
                "type": "Element",
            }
        )
        sort_name: str = field(
            metadata={
                "name": "sort-name",
                "type": "Element",
            }
        )

    @dataclass(kw_only=True)
    class Gender:
        class Meta:
            name = "gender"
            namespace = "http://musicbrainz.org/ns/mmd-2.0#"

        id: str = field(
            metadata={
                "type": "Attribute",
            }
        )
        value: str = field(default="")

    @dataclass(kw_only=True)
    class IpiList:
        class Meta:
            name = "ipi-list"
            namespace = "http://musicbrainz.org/ns/mmd-2.0#"

        ipi: list[str] = field(
            default_factory=list,
            metadata={
                "type": "Element",
                "min_occurs": 1,
            },
        )

    @dataclass(kw_only=True)
    class IsniList:
        class Meta:
            name = "isni-list"
            namespace = "http://musicbrainz.org/ns/mmd-2.0#"

        isni: list[str] = field(
            default_factory=list,
            metadata={
                "type": "Element",
                "min_occurs": 1,
            },
        )

    @dataclass(kw_only=True)
    class Iso31661CodeList:
        class Meta:
            name = "iso-3166-1-code-list"
            namespace = "http://musicbrainz.org/ns/mmd-2.0#"

        iso_3166_1_code: str = field(
            metadata={
                "name": "iso-3166-1-code",
                "type": "Element",
            }
        )

    @dataclass(kw_only=True)
    class Iso31662CodeList:
        class Meta:
            name = "iso-3166-2-code-list"
            namespace = "http://musicbrainz.org/ns/mmd-2.0#"

        iso_3166_2_code: str = field(
            metadata={
                "name": "iso-3166-2-code",
                "type": "Element",
            }
        )

    @dataclass(kw_only=True)
    class LifeSpan:
        class Meta:
            name = "life-span"
            namespace = "http://musicbrainz.org/ns/mmd-2.0#"

        begin: XmlDate | XmlPeriod = field(
            metadata={
                "type": "Element",
            }
        )
        end: None | XmlDate = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        ended: None | bool = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )

    @dataclass(kw_only=True)
    class AliasList:
        class Meta:
            name = "alias-list"
            namespace = "http://musicbrainz.org/ns/mmd-2.0#"

        count: int = field(
            metadata={
                "type": "Attribute",
            }
        )
        alias: list[Alias] = field(
            default_factory=list,
            metadata={
                "type": "Element",
                "min_occurs": 1,
            },
        )

    @dataclass(kw_only=True)
    class Area:
        class Meta:
            name = "area"
            namespace = "http://musicbrainz.org/ns/mmd-2.0#"

        id: str = field(
            metadata={
                "type": "Attribute",
            }
        )
        name: str = field(
            metadata={
                "type": "Element",
            }
        )
        sort_name: str = field(
            metadata={
                "name": "sort-name",
                "type": "Element",
            }
        )
        iso_3166_1_code_list: Iso31661CodeList = field(
            metadata={
                "name": "iso-3166-1-code-list",
                "type": "Element",
            }
        )

    @dataclass(kw_only=True)
    class EndArea:
        class Meta:
            name = "end-area"
            namespace = "http://musicbrainz.org/ns/mmd-2.0#"

        id: str = field(
            metadata={
                "type": "Attribute",
            }
        )
        name: str = field(
            metadata={
                "type": "Element",
            }
        )
        sort_name: str = field(
            metadata={
                "name": "sort-name",
                "type": "Element",
            }
        )
        iso_3166_2_code_list: Iso31662CodeList = field(
            metadata={
                "name": "iso-3166-2-code-list",
                "type": "Element",
            }
        )

    @dataclass(kw_only=True)
    class Artist:
        class Meta:
            name = "artist"
            namespace = "http://musicbrainz.org/ns/mmd-2.0#"

        id: str = field(
            metadata={
                "type": "Attribute",
            }
        )
        type_value: str = field(
            metadata={
                "name": "type",
                "type": "Attribute",
            }
        )
        type_id: str = field(
            metadata={
                "name": "type-id",
                "type": "Attribute",
            }
        )
        name: str = field(
            metadata={
                "type": "Element",
            }
        )
        sort_name: str = field(
            metadata={
                "name": "sort-name",
                "type": "Element",
            }
        )
        disambiguation: None | str = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        ipi: None | str = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        ipi_list: None | IpiList = field(
            default=None,
            metadata={
                "name": "ipi-list",
                "type": "Element",
            },
        )
        isni_list: IsniList = field(
            metadata={
                "name": "isni-list",
                "type": "Element",
            }
        )
        gender: None | Gender = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        country: str = field(
            metadata={
                "type": "Element",
            }
        )
        area: Area = field(
            metadata={
                "type": "Element",
            }
        )
        begin_area: BeginArea = field(
            metadata={
                "name": "begin-area",
                "type": "Element",
            }
        )
        end_area: None | EndArea = field(
            default=None,
            metadata={
                "name": "end-area",
                "type": "Element",
            },
        )
        life_span: LifeSpan = field(
            metadata={
                "name": "life-span",
                "type": "Element",
            }
        )
        alias_list: AliasList = field(
            metadata={
                "name": "alias-list",
                "type": "Element",
            }
        )

    @dataclass(kw_only=True)
    class Metadata:
        class Meta:
            name = "metadata"
            namespace = "http://musicbrainz.org/ns/mmd-2.0#"

        artist: Artist = field(
            metadata={
                "type": "Element",
            }
        )

## JSON Documents

    $ xsdata generate --package tests.fixtures.series tests/fixtures/series/samples

Sample #1 Sample #2 Output

    {
      "id": 2,
      "name": "Person of Interest",
      "type": "Scripted",
      "language": "English",
      "genres": [
        "Action",
        "Crime",
        "Science-Fiction"
      ],
      "status": "Ended",
      "runtime": 60,
      "premiered": "2011-09-22",
      "officialSite": "http://www.cbs.com/shows/person_of_interest/",
      "schedule": {
        "time": "22:00",
        "days": [
          "Tuesday"
        ]
      },
      "rating": {
        "average": 8.9
      },
      "network": {
        "id": 2,
        "name": "CBS",
        "country": {
          "name": "United States",
          "code": "US",
          "timezone": "America/New_York"
        }
      },
      "webChannel": null,
      "externals": {
        "tvrage": 28376,
        "imdb": "tt1839578"
      },
      "summary": "<p>You are being watched. The government has a secret system, a machine that spies on you every hour of every day. I know because I built it. I designed the Machine to detect acts of terror but it sees everything. Violent crimes involving ordinary people. People like you. Crimes the government considered \"irrelevant\". They wouldn't act so I decided I would. But I needed a partner. Someone with the skills to intervene. Hunted by the authorities, we work in secret. You'll never find us. But victim or perpetrator, if your number is up, we'll find you.</p>",
      "updated": 1588773151
    }

    {
      "id": 5,
      "url": "https://www.tvmaze.com/shows/5/true-detective",
      "name": "True Detective",
      "type": "Scripted",
      "language": "English",
      "genres": [
        "Drama",
        "Crime",
        "Thriller"
      ],
      "status": "Running",
      "runtime": 60,
      "premiered": "2014-01-12",
      "officialSite": "http://www.hbo.com/true-detective",
      "schedule": {
        "time": "21:00",
        "days": [
          "Sunday"
        ]
      },
      "rating": {
        "average": 8.3
      },
      "weight": 97,
      "network": {
        "name": "HBO",
        "country": {
          "name": "United States",
          "code": "US"
        }
      },
      "externals": {
        "tvrage": 31369,
        "thetvdb": 270633,
        "imdb": "tt2356777"
      },
      "image": {
        "medium": "https://static.tvmaze.com/uploads/images/medium_portrait/178/445621.jpg",
        "original": "https://static.tvmaze.com/uploads/images/original_untouched/178/445621.jpg"
      },
      "summary": "<p>Touch darkness and darkness touches you back. <b>True Detective</b> centers on troubled cops and the investigations that drive them to the edge. Each season features a new cast and a new case.</p>",
      "updated": 1614762066,
      "_links": {
        "self": {
          "href": "https://api.tvmaze.com/shows/5"
        },
        "previousepisode": {
          "href": "https://api.tvmaze.com/episodes/1590271"
        }
      }
    }

    from __future__ import annotations

    from dataclasses import dataclass, field

    from xsdata.models.datatype import XmlDate

    @dataclass(kw_only=True)
    class Country:
        class Meta:
            name = "country"

        name: str = field(
            metadata={
                "type": "Element",
            }
        )
        code: str = field(
            metadata={
                "type": "Element",
            }
        )
        timezone: None | str = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )

    @dataclass(kw_only=True)
    class Externals:
        class Meta:
            name = "externals"

        tvrage: int = field(
            metadata={
                "type": "Element",
            }
        )
        thetvdb: None | int = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        imdb: str = field(
            metadata={
                "type": "Element",
            }
        )

    @dataclass(kw_only=True)
    class Image:
        class Meta:
            name = "image"

        medium: str = field(
            metadata={
                "type": "Element",
            }
        )
        original: str = field(
            metadata={
                "type": "Element",
            }
        )

    @dataclass(kw_only=True)
    class Previousepisode:
        class Meta:
            name = "previousepisode"

        href: str = field(
            metadata={
                "type": "Element",
            }
        )

    @dataclass(kw_only=True)
    class Rating:
        class Meta:
            name = "rating"

        average: float = field(
            metadata={
                "type": "Element",
            }
        )

    @dataclass(kw_only=True)
    class Schedule:
        class Meta:
            name = "schedule"

        time: str = field(
            metadata={
                "type": "Element",
            }
        )
        days: list[str] = field(
            default_factory=list,
            metadata={
                "type": "Element",
                "min_occurs": 1,
            },
        )

    @dataclass(kw_only=True)
    class Self:
        class Meta:
            name = "self"

        href: str = field(
            metadata={
                "type": "Element",
            }
        )

    @dataclass(kw_only=True)
    class Links:
        class Meta:
            name = "_links"

        self_value: Self = field(
            metadata={
                "name": "self",
                "type": "Element",
            }
        )
        previousepisode: Previousepisode = field(
            metadata={
                "type": "Element",
            }
        )

    @dataclass(kw_only=True)
    class Network:
        class Meta:
            name = "network"

        id: None | int = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        name: str = field(
            metadata={
                "type": "Element",
            }
        )
        country: Country = field(
            metadata={
                "type": "Element",
            }
        )

    @dataclass(kw_only=True)
    class Series:
        class Meta:
            name = "series"

        id: int = field(
            metadata={
                "type": "Element",
            }
        )
        url: None | str = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        name: str = field(
            metadata={
                "type": "Element",
            }
        )
        type_value: str = field(
            metadata={
                "name": "type",
                "type": "Element",
            }
        )
        language: str = field(
            metadata={
                "type": "Element",
            }
        )
        genres: list[str] = field(
            default_factory=list,
            metadata={
                "type": "Element",
                "min_occurs": 1,
            },
        )
        status: str = field(
            metadata={
                "type": "Element",
            }
        )
        runtime: int = field(
            metadata={
                "type": "Element",
            }
        )
        premiered: XmlDate = field(
            metadata={
                "type": "Element",
            }
        )
        official_site: str = field(
            metadata={
                "name": "officialSite",
                "type": "Element",
            }
        )
        schedule: Schedule = field(
            metadata={
                "type": "Element",
            }
        )
        rating: Rating = field(
            metadata={
                "type": "Element",
            }
        )
        weight: None | int = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        network: Network = field(
            metadata={
                "type": "Element",
            }
        )
        web_channel: None | object = field(
            default=None,
            metadata={
                "name": "webChannel",
                "type": "Element",
            },
        )
        externals: Externals = field(
            metadata={
                "type": "Element",
            }
        )
        image: None | Image = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        summary: str = field(
            metadata={
                "type": "Element",
            }
        )
        updated: int = field(
            metadata={
                "type": "Element",
            }
        )
        links: None | Links = field(
            default=None,
            metadata={
                "name": "_links",
                "type": "Element",
            },
        )