Converts a Python dictionary or other native data type into a valid XML string.
86
Convert nested catalog-style data into an XML document that preserves hierarchy across mappings and sequences.
{"departments": [{"name": "Books", "categories": [{"name": "Fiction", "products": ["Novel", "Shorts"]}]}]}, return an XML document rooted at catalog with an XML declaration and type metadata on each element. Nested content should appear as <catalog><departments type="list"><department>...<categories type="list"><category>...<products type="list"><product>Novel</product><product>Shorts</product></products></category></categories></department></departments></catalog>, preserving the hierarchy. @testroot_tag="inventory", include_types=False, and as_bytes=False on {"departments": []}, return a string starting with <inventory><departments></departments></inventory> (no type attributes anywhere). @test{"departments": [{"name": "Clothing", "categories": []}, {"name": "Electronics", "categories": [{"name": "Cameras", "products": []}]}]} should produce empty tags for the missing categories/products while keeping enclosing elements (e.g., <categories></categories> and <products></products>) without raising errors. @test@generates
from typing import Any, Mapping
def render_catalog_xml(
data: Mapping[str, Any],
*,
root_tag: str = "catalog",
include_types: bool = True,
as_bytes: bool = True,
singularize_items: bool = True,
):
"""Serialize nested catalog data into XML with predictable item naming."""Converts Python mappings and sequences into XML with optional metadata and configurable element naming. @satisfied-by
Install with Tessl CLI
npx tessl i tessl/pypi-dicttoxmldocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10