From c069cf87bd94b7916f8533ed299c1f5b12b6d1ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20M=C3=B6ller?= Date: Tue, 19 Dec 2023 13:37:39 +0100 Subject: [PATCH] adapter.xml: improve element formatting in error messages Previously, the namespace of an element would always be replaced by its prefix if the prefix is known. However, this turned out to mask errors in case the namespace is different from the one used by our SDK. Thus, the function `_element_pretty_identifier()` is adjusted such that it only replaces the namespace if it matches one of the namespaces known to our SDK. Partially fix https://github.com/eclipse-basyx/basyx-python-sdk/issues/190 See also: 79a86352bb52b1528a9bf9c860331915b4db7556 --- basyx/aas/adapter/xml/xml_deserialization.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/basyx/aas/adapter/xml/xml_deserialization.py b/basyx/aas/adapter/xml/xml_deserialization.py index 535d029a4..d99763c77 100644 --- a/basyx/aas/adapter/xml/xml_deserialization.py +++ b/basyx/aas/adapter/xml/xml_deserialization.py @@ -97,7 +97,7 @@ def _element_pretty_identifier(element: etree.Element) -> str: Returns a pretty element identifier for a given XML element. If the prefix is known, the namespace in the element tag is replaced by the prefix. - If additionally also the sourceline is known, is is added as a suffix to name. + If additionally also the sourceline is known, it is added as a suffix to name. For example, instead of "{http://www.admin-shell.io/aas/2/0}assetAdministrationShell" this function would return "aas:assetAdministrationShell on line $line", if both, prefix and sourceline, are known. @@ -106,7 +106,11 @@ def _element_pretty_identifier(element: etree.Element) -> str: """ identifier = element.tag if element.prefix is not None: - identifier = element.prefix + ":" + element.tag.split("}")[1] + # Only replace the namespace by the prefix if it matches our known namespaces, + # so the replacement by the prefix doesn't mask errors such as incorrect namespaces. + namespace, tag = element.tag.split("}", 1) + if namespace[1:] in NS_MAP.values(): + identifier = element.prefix + ":" + tag if element.sourceline is not None: identifier += f" on line {element.sourceline}" return identifier