IDE: Better xmlclass behavior with simple XML element having no attibutes (simple TAGs).
authorEdouard Tisserant <edouard@beremiz.fr>
Thu, 16 Jan 2025 14:52:49 +0100 (8 weeks ago)
changeset 4096 d459b9179ec3
parent 4085 8d150133d225
child 4097 08ab4ad50346
IDE: Better xmlclass behavior with simple XML element having no attibutes (simple TAGs).

xsd:choice can sometime lead to such situation when there is no additional data associated to a particular choice.
xmlclass/xmlclass.py
--- a/xmlclass/xmlclass.py	Thu Jan 09 17:11:18 2025 +0100
+++ b/xmlclass/xmlclass.py	Thu Jan 16 14:52:49 2025 +0100
@@ -575,7 +575,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"])
@@ -593,15 +593,14 @@
         else:
             return ""
 
-    def Initial():
-        p = etree.Element(infos["name"])
-        return p
+    def InitialTag():
+        return factory.Parser.CreateElement(infos["name"])
 
     return {
         "type": TAG,
         "extract": ExtractTag,
         "generate": GenerateTag,
-        "initial": Initial,
+        "initial": InitialTag,
         "check": lambda x: x is None or infos["minOccurs"] == 0 and x
     }
 
@@ -659,7 +658,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"])
@@ -1147,7 +1146,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"])
@@ -1489,8 +1488,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,
@@ -1736,7 +1734,14 @@
         return NAMESPACE_PATTERN.sub("", etree.tostring(self, encoding='unicode'))
 
     def getElementInfos(self, name, path=None, derived=False):
-        return {"name": name, "type": TAG, "value": None, "use": None, "children": []}
+        if path is not None:
+            raise ValueError("Simple element "+name+" accepts no path: "+path)
+        return {
+            "name": name,
+            "type": "element",
+            "value": None,
+            "use": "required",
+            "children": []}
 
 class XMLElementClassLookUp(etree.PythonElementClassLookup):