beremiz

6dcf54a4f5f0
Parents 9ade1b678069
Children 1e8869ed2d4c
Fix xmlclass not behaving with simple XML element having to attibutes.

xsd:choice can sometime lead to such situation when there is no additional data associated to a particular choice.
--- a/xmlclass/xmlclass.py Wed Dec 11 09:31:35 2024 +0100
+++ b/xmlclass/xmlclass.py Thu Jan 16 14:52:49 2025 +0100
@@ -580,7 +580,7 @@
}
-def GenerateTagInfos(infos):
+def GenerateTagInfos(factory, infos):
def ExtractTag(tree):
if len(tree._attrs) > 0:
raise ValueError("\"%s\" musn't have attributes!" % infos["name"])
@@ -598,11 +598,14 @@
else:
return ""
+ def InitialTag():
+ return factory.Parser.CreateElement(infos["name"])
+
return {
"type": TAG,
"extract": ExtractTag,
"generate": GenerateTag,
- "initial": lambda: None,
+ "initial": InitialTag,
"check": lambda x: x is None or infos["minOccurs"] == 0 and x
}
@@ -660,7 +663,7 @@
if element_infos is not None:
sequence_element["elmt_type"] = element_infos
elif choice["elmt_type"] == "tag":
- choice["elmt_type"] = GenerateTagInfos(choice)
+ choice["elmt_type"] = GenerateTagInfos(factory, choice)
factory.AddToLookupClass(choice["name"], name, DefaultElementClass)
else:
choice_infos = factory.ExtractTypeInfos(choice["name"], name, choice["elmt_type"])
@@ -1146,7 +1149,7 @@
else:
elmtname = element["name"]
if element["elmt_type"] == "tag":
- infos = GenerateTagInfos(element)
+ infos = GenerateTagInfos(self, element)
self.AddToLookupClass(element["name"], name, DefaultElementClass)
else:
infos = self.ExtractTypeInfos(element["name"], name, element["elmt_type"])
@@ -1492,8 +1495,7 @@
value = ""
else:
value = self.content.getLocalTag()
- if self.content is not None:
- children.extend(self.content.getElementInfos(value)["children"])
+ children.extend(self.content.getElementInfos(value)["children"])
elif element["elmt_type"]["type"] == SIMPLETYPE:
children.append({
"name": element_name,
@@ -1738,6 +1740,15 @@
def tostring(self):
return NAMESPACE_PATTERN.sub("", etree.tostring(self, pretty_print=True, encoding='utf-8')).decode('utf-8')
+ def getElementInfos(self, name, path=None, derived=False):
+ if path is not None:
+ raise ValueError("Wrong path!")
+ return {
+ "name": name,
+ "type": "element",
+ "value": None,
+ "use": "required",
+ "children": []}
class XMLElementClassLookUp(etree.PythonElementClassLookup):