Open ghilesmeddour opened 1 week ago
@ghilesmeddour Hi,
Namespaces are not inherited by nested models, so they must be explicitly defined for all models:
from typing import Optional
from uuid import UUID
from pydantic_xml import BaseXmlModel, attr, element, wrapped
NSMAP = {"": "urn:hl7-org:v3"}
class EffectiveTime(BaseXmlModel, tag="effectiveTime", nsmap=NSMAP, search_mode="unordered"):
center: str = wrapped("center", attr(name="value"))
class Code(BaseXmlModel, tag="code", nsmap=NSMAP, search_mode="unordered"):
code: str = attr()
code_system: str = attr(name="codeSystem")
code_system_name: Optional[str] = attr(name="codeSystemName", default=None)
display_name: Optional[str] = attr(name="displayName", default=None)
class AnnotatedECG(BaseXmlModel, nsmap=NSMAP, search_mode="unordered"):
id: UUID = wrapped("id", attr(name="root"))
code: Code
text: Optional[str] = element(default=None)
effective_time: EffectiveTime
# This is working
# center: str = wrapped("effectiveTime/center", attr(name="value"))
Thank you very much @dapper91 for your help.
I'll take the opportunity to ask another question. The input files can have different namespaces ([None, "urn:hl7-org:v1", "urn:hl7-org:v2", "urn:hl7-org:v3"]
) and I want to parse them the same way. Is there any way to do this transparently, to ignore namespaces completely or to specify a namespace at parsing time (as a from_xml
parameter for example) and not at models definition.
Hi @dapper91 again :blush:,
I'm trying to use the lib to parse HL7 aECG files.
Let's take this file as an example.
I have the following program:
And I have the following error:
I can't understand why the error occurs (especially since the commented line, which is equivalent for me, works correctly). What am I doing wrong?