Fix bug in xmlclass with multiple choices in sequence
authorlaurent
Mon, 30 Apr 2012 16:27:25 +0200
changeset 674 bbffe4110141
parent 673 b686f0081e2b
child 675 0ea836add01f
Fix bug in xmlclass with multiple choices in sequence
xmlclass/xmlclass.py
xmlclass/xsdschema.py
--- a/xmlclass/xmlclass.py	Fri Apr 27 02:00:47 2012 +0200
+++ b/xmlclass/xmlclass.py	Mon Apr 30 16:27:25 2012 +0200
@@ -726,13 +726,26 @@
                 element_idx = 0
                 while element_idx < len(value["value"]):
                     for element_infos in infos["elements"]:
-                        if element_infos["name"] == value["value"][element_idx]["name"]:
-                            element_value = value["value"][element_idx]["value"]
-                            element_idx += 1
+                        element_value = None
+                        if element_infos["type"] == CHOICE:
+                            choice_infos = None
+                            if element_idx < len(value["value"]):
+                                for choice in element_infos["choices"]:
+                                    if choice["name"] == value["value"][element_idx]["name"]:
+                                        choice_infos = choice
+                                        element_value = value["value"][element_idx]["value"]
+                                        element_idx += 1
+                                        break
+                            if ((choice_infos is not None and 
+                                 not CheckElementValue(factory, choice_infos["name"], choice_infos, element_value, False)) or
+                                (choice_infos is None and element_infos["minOccurs"] > 0)):
+                                raise ValueError("Invalid sequence value in attribute 'content'")
                         else:
-                            element_value = None
-                        if not CheckElementValue(factory, element_infos["name"], element_infos, element_value, False):
-                            raise ValueError("Invalid sequence value in attribute 'content'")
+                            if element_idx < len(value["value"]) and element_infos["name"] == value["value"][element_idx]["name"]:
+                                element_value = value["value"][element_idx]["value"]
+                                element_idx += 1
+                            if not CheckElementValue(factory, element_infos["name"], element_infos, element_value, False):
+                                raise ValueError("Invalid sequence value in attribute 'content'")
                     sequence_number += 1
                 if sequence_number < infos["minOccurs"] or infos["maxOccurs"] != "unbounded" and sequence_number > infos["maxOccurs"]:
                     raise ValueError("Invalid sequence value in attribute 'content'")
--- a/xmlclass/xsdschema.py	Fri Apr 27 02:00:47 2012 +0200
+++ b/xmlclass/xsdschema.py	Mon Apr 30 16:27:25 2012 +0200
@@ -807,6 +807,10 @@
                     choices_tmp.append(choice)
             choices.extend(choices_tmp)
     
+    for choice in choices:
+        attributes["minOccurs"] = min(attributes["minOccurs"], choice["minOccurs"])
+        choice["minOccurs"] = 1
+    
     return {"type": CHOICE, "choices": choices, "minOccurs": attributes["minOccurs"],
             "maxOccurs": attributes["maxOccurs"], "doc": annotations}