Element
This class represents a structured document element consisting of a name, a sorted list of attributes, and a collection of child nodes. It provides functionality for appending content with automatic whitespace normalization, finalizing the element tree, and performing deep equality comparisons. Additionally, the class supports containment checks and occurrence counting for both text content and nested element structures.
Attributes
| Attribute | Type | Description |
|---|---|---|
| name | string | The tag name of the element used for identification and string representation. |
| attributes | list | A sorted list of key-value pairs representing the element's HTML or XML attributes. |
| children | list = [] | A list containing nested Element objects or strings representing the inner content of the element. |
Constructor
Signature
def Element(
name: string,
attributes: iterable
)
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | The name of the element. |
| attributes | iterable | A collection of attribute key-value pairs to be sorted and stored. |
Methods
append()
@classmethod
def append(
element: [Union](../../contrib/gis/db/models/functions/union.md?sid=django_contrib_gis_db_models_functions_union)[[Element](element.md?sid=django_test_html_element), str]
) - > null
Adds a child element or text string to this element, normalizing whitespace and merging adjacent text nodes.
Parameters
| Name | Type | Description |
|---|---|---|
| element | [Union](../../contrib/gis/db/models/functions/union.md?sid=django_contrib_gis_db_models_functions_union)[[Element](element.md?sid=django_test_html_element), str] | The child element or text content to be added to the internal children list. |
Returns
| Type | Description |
|---|---|
null | Nothing |
finalize()
@classmethod
def finalize() - > null
Recursively cleans up the element tree by stripping trailing whitespace from text nodes and removing empty text children.
Returns
| Type | Description |
|---|---|
null | Nothing |
count()
@classmethod
def count(
element: [Union](../../contrib/gis/db/models/functions/union.md?sid=django_contrib_gis_db_models_functions_union)[[Element](element.md?sid=django_test_html_element), str]
) - > int
Calculates the total number of times a specific element or text string appears within this element's hierarchy.
Parameters
| Name | Type | Description |
|---|---|---|
| element | [Union](../../contrib/gis/db/models/functions/union.md?sid=django_contrib_gis_db_models_functions_union)[[Element](element.md?sid=django_test_html_element), str] | The target element or text string to be counted. |
Returns
| Type | Description |
|---|---|
int | The total count of matching elements or text occurrences. |