Skip to content

_parse_tree raises ValueError for non-XsdElement children in XSD schemas #47

@moizahmed-de

Description

@moizahmed-de

Description

When parsing certain XSD schemas (specifically ISO 20022), the _parse_tree method in model.py raises a ValueError because it encounters a child node that is not an xmlschema.XsdElement.

The relevant code at model.py line 541:

for child in parent_node:
    if type(child) is xmlschema.XsdElement:
        # ... handles XsdElement children
    else:
        raise ValueError("unknown case; please check (child not an XsdElement)")

This does not handle other valid XSD constructs that xmlschema may yield when iterating over a node's children (e.g., XsdAny, XsdGroup, or other non-element particles).

Steps to Reproduce

  1. Use an XSD schema for ISO 20022
  2. Attempt to create a DataModel from this XSD:
from xml2db import DataModel

model = DataModel(
    xsd_file="path/to/xsd",
)
  1. The ValueError is raised during _build_model()_parse_tree()

Error Traceback

Traceback (most recent call last):
  File ".../xml2db/model.py", line 136, in __init__
    self._build_model()
  File ".../xml2db/model.py", line 228, in _build_model
    root_table = self._parse_tree(
        self.xml_schema[0] if len(self.xml_schema) == 1 else self.xml_schema
    )
  File ".../xml2db/model.py", line 517, in _parse_tree
    child_table = self._parse_tree(child, nodes_path)
  File ".../xml2db/model.py", line 541, in _parse_tree
    raise ValueError("unknown case; please check (child not an XsdElement)")
ValueError: unknown case; please check (child not an XsdElement)

Workaround

Replacing the raise ValueError(...) with a warning allows the pipeline to proceed (skipping the unrecognized child):

else:
    logger.warning(
        f"Skipping non-XsdElement child: type={type(child).__name__}, "
        f"child={child}, parent={parent_name}"
    )

Expected Behavior

_parse_tree should handle non-XsdElement children gracefully — either by skipping them with a warning, or by implementing proper support for the relevant XSD constructs (e.g., xs:any, xs:group, etc.).

Environment

  • xml2db version: 0.12.5
  • Python version: 3.14.2
  • xmlschema version: 4.3.1
  • OS: Windows

The XSD in question is a standard ISO 20022. These schemas are widely used and would be a valuable addition to xml2db's supported XSD features.

The fix in PR #17 (v0.12.4) addressed several XSD parsing improvements (recursive definitions, xsd:list, unbounded sequences), but this specific case of non-XsdElement children was not covered.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions