Homogeneous collections#
Homogeneous collection is a collection of same type elements.
The most common homogeneous collections are typing.List
, typing.Set
and
variable-length tuple typing.Tuple
(like Tuple[int, ...]
)
Primitive homogeneous collection#
Field of a primitive homogeneous collection type marked as pydantic_xml.element()
is bound
to sub-elements text.
class Company(BaseXmlModel):
products: List[str] = element(tag='Product')
<Company>
<Product>Several launch vehicles</Product>
<Product>Starlink</Product>
<Product>Starship</Product>
</Company>
{
"products": [
"Several launch vehicles",
"Starlink",
"Starship"
]
}
Model homogeneous collection#
Field of a model homogeneous collection type is bound to sub-elements. Then the sub-element is used
as a root for that sub-model. For more information see model data binding.
Parameter tag
is used to declare a sub-elements tag to which the sub-models are bound.
If it is omitted the sub-model tag
parameter is used.
If it is omitted too field name is used (respecting pydantic
field aliases).
class Social(BaseXmlModel):
type: str = attr()
url: str
class Product(BaseXmlModel):
status: Literal['running', 'development'] = attr()
launched: Optional[int] = attr()
title: str
class Company(BaseXmlModel):
socials: Tuple[Social, ...] = element(tag='social')
products: List[Product] = element(tag='product')
<Company>
<social type="linkedin">https://www.linkedin.com/company/spacex</social>
<social type="twitter">https://twitter.com/spacex</social>
<social type="youtube">https://www.youtube.com/spacex</social>
<product status="running" launched="2013">Several launch vehicles</product>
<product status="running" launched="2019">Starlink</product>
<product status="development">Starship</product>
</Company>
{
"socials": [
{
"type": "linkedin",
"url": "https://www.linkedin.com/company/spacex"
},
{
"type": "twitter",
"url": "https://twitter.com/spacex"
},
{
"type": "youtube",
"url": "https://www.youtube.com/spacex"
}
],
"products": [
{
"status": "running",
"launched": 2013,
"title": "Several launch vehicles"
},
{
"status": "running",
"launched": 2019,
"title": "Starlink"
},
{
"status": "development",
"title": "Starship"
}
]
}
Dict homogeneous collection#
Field of a mapping homogeneous collection type is bound to sub-elements attributes:
class Company(BaseXmlModel):
products: List[Dict[str, str]] = element(tag='product')
<Company>
<product status="running" launched="2013"/>
<product status="running" launched="2019"/>
<product status="development"/>
</Company>
{
"products": [
{
"status": "running",
"launched": 2013
},
{
"status": "running",
"launched": 2019
},
{
"status": "development"
}
]
}