# Builders Module
The `builders` package provides typed APIs for constructing TreeStore hierarchies.
## Module Structure
```{mermaid}
graph TB
subgraph "genro_treestore.builders"
BASE[base.py
BuilderBase]
DEC[decorators.py
@element]
HTML[html.py
HtmlBuilder]
subgraph "xsd/"
XSD[XsdBuilder]
end
BASE --> HTML
BASE --> XSD
DEC --> BASE
end
```
## BuilderBase
```{eval-rst}
.. autoclass:: genro_treestore.BuilderBase
:members:
:undoc-members:
:show-inheritance:
```
## Decorators
### @element
```{eval-rst}
.. autofunction:: genro_treestore.element
```
```{note}
The `element` decorator handles both tag registration and children validation via the `children` parameter.
```
## HtmlBuilder
```{eval-rst}
.. autoclass:: genro_treestore.HtmlBuilder
:members:
:undoc-members:
:show-inheritance:
```
### HTML5 Content Model
```{mermaid}
graph TB
subgraph "Content Categories"
FLOW[Flow Content
div, p, section...]
PHRASING[Phrasing Content
span, a, em...]
EMBEDDED[Embedded Content
img, video, iframe...]
INTERACTIVE[Interactive Content
a, button, input...]
METADATA[Metadata Content
head, title, meta...]
end
FLOW --> PHRASING
FLOW --> EMBEDDED
FLOW --> INTERACTIVE
```
## XsdBuilder
Dynamic builder from XML Schema (XSD) files.
```{eval-rst}
.. autoclass:: genro_treestore.XsdBuilder
:members:
:undoc-members:
:show-inheritance:
```
### Example
```python
from genro_treestore import TreeStore
from genro_treestore.builders import XsdBuilder
# Load XSD schema
xsd_content = open('invoice.xsd').read()
schema = TreeStore.from_xml(xsd_content)
builder = XsdBuilder(schema)
# Build validated structure
store = TreeStore(builder=builder)
invoice = store.Invoice()
invoice.Header().Date(value='2025-01-01')
```
## Builder Architecture
```{mermaid}
classDiagram
class BuilderBase {
<>
+_schema: dict
+child(tag, **attr)
+__getattr__(name)
}
class HtmlBuilder {
+div(**attr)
+span(**attr)
+p(**attr)
...
}
class XsdBuilder {
+__init__(schema)
}
BuilderBase <|-- HtmlBuilder
BuilderBase <|-- XsdBuilder
```
## Validation Flow
```{mermaid}
flowchart TD
ADD[Add child node]
CHK{Valid child?}
CARD{Cardinality OK?}
OK[Node added]
ERR[InvalidChildError]
CERR[TooManyChildrenError]
ADD --> CHK
CHK -->|Yes| CARD
CHK -->|No| ERR
CARD -->|Yes| OK
CARD -->|No| CERR
```
## Cardinality Syntax
| Syntax | Meaning | Example |
|--------|---------|---------|
| `tag` | Zero or more | `'div'` |
| `tag[:]` | Zero or more | `'div[:]'` |
| `tag[1]` | Exactly one | `'title[1]'` |
| `tag[0:1]` | Zero or one | `'subtitle[0:1]'` |
| `tag[1:]` | One or more | `'section[1:]'` |
| `tag[2:5]` | Range | `'item[2:5]'` |
## See Also
- [Builders Guide](../guide/builders.md) - Creating custom builders
- [Validation Guide](../guide/validation.md) - Validation rules