Mappings#

Local attributes#

A field of a mapping type is bound to local element attributes.

Model
class Company(BaseXmlModel):
    properties: Dict[str, str]
Document
<Company trade-name="SpaceX" type="Private"/>
{
    "properties": {
        "trade-name": "SpaceX",
        "type": "Private"
    }
}

Sub-element attributes#

A field of a mapping type marked as pydantic_xml.element() is bound to sub-element attributes. The tag parameter of pydantic_xml.element() is used as a sub-element tag to which attributes the field is bound. If it is omitted the field name is used (respecting pydantic field aliases).

Model
class Company(BaseXmlModel):
    founder: Dict[str, str] = element(tag='Founder')
Document
<Company>
    <Founder name="Elon" surname="Musk"/>
</Company>
{
    "founder": {
        "name": "Elon",
        "surname": "Musk"
    }
}

Typed dict#

Fields of typing.TypedDict type are also supported:

Model
class Information(TypedDict):
    founded: dt.date
    employees: int
    website: HttpUrl


class Company(BaseXmlModel):
    info: Information
Document
<Company founded="2002-03-14"
         employees="12000"
         website="https://www.spacex.com"/>
{
    "info": {
        "founded": "2002-03-14",
        "employees": 12000,
        "website": "https://www.spacex.com"
    }
}