Commits

Sergey A. Bozhenkov  committed 5f17123

Fix issue 11 - schema element where compplexType/simpleType is not the first child of the element

  • Participants
  • Parent commits bcdeafb

Comments (0)

Files changed (3)

File osa/xmlschema.py

         # complexContent.extension - complex class with parent
         # simpleType.restriction - e.g. string enumeration
         if element.tag == '{%s}element' % xmlnamespace.NS_XSD:
-            if len(element) > 0:
-                element[0].set('qualified', element.get('qualified', 0))
-                element = element[0]
-            else:
+            children = element.findall('./{%s}complexType' % xmlnamespace.NS_XSD)
+            children.extend(element.findall('./{%s}simpleType' % xmlnamespace.NS_XSD))
+            if len(children) == 1:
+                children[0].set('qualified', element.get('qualified', 0))
+                element = children[0]
+            elif len(children) == 0:
                 type = element.get('type', None)
                 if type is not None:
                     # alias
                     # empty class
                     XMLSchemaParser.create_empty_class(name, types)
                     return
+            else:
+                raise ValueError(' Wrong schema structure. Element has more than 1 '
+                        ' child specifying the class. Element: %s' % (name))
         if element.tag == '{%s}complexType' % xmlnamespace.NS_XSD:
             # complex class
             XMLSchemaParser.create_complex_class(name, element, xtypes, types)

File tests/schema.xml

   </element>
   <element name="Robot" type="tns:Person"/>
   <element name="Shish" />
+  <element name = "PersonDoc">
+      <annotation> bla </annotation>
+      <complexType>
+           <sequence>
+             <element name="age" type="xsd:int" minOccurs="1" maxOccurs="1"/>
+             <element name="height" type="xsd:int" minOccurs="1" maxOccurs="1"/>
+             <element name="weight" type="xsd:float" minOccurs="1" maxOccurs="1"/>
+             <element name="name" type="tns:Name" minOccurs="0" maxOccurs="1" nillable="true"/>
+             <element name="place" type="x:Address" minOccurs ="1" maxOccurs="10"/>
+             <element name="car" type="y:Car" minOccurs ="0" maxOccurs="5"/>
+             <element ref="tns:Comment"/>
+           </sequence>
+      </complexType>
+  </element>
+  <element name = "CounterDoc">
+      <annotation> bla </annotation>
+      <simpleType name="Counter">
+          <restriction base="int">
+              <minInclusive value="0"/>
+              <maxInclusive value="120"/>
+          </restriction>
+      </simpleType>
+  </element>
+  <element name="RobotDoc" type="tns:Person">
+      <annotation> bla </annotation>
+  </element>
+  <element name="ShishDoc" >
+      <annotation> bla </annotation>
+  </element>
 </schema>

File tests/test_xmlschema.py

         self.assertTrue("{sever}Car" in res)
         self.assertEqual(res["{sever}Car"].tag, "{%s}complexType" %xmlnamespace.NS_XSD)
         self.assertEqual(res["{sever}Car"].get("name"), "Car")
+        # added after fixing issue 11 - present annotation in element tag
+        self.assertEqual(res["{vostok}PersonDoc"].tag, "{%s}element" %xmlnamespace.NS_XSD)
+        self.assertEqual(res["{vostok}PersonDoc"].get("name"), "PersonDoc")
+        self.assertEqual(res["{vostok}CounterDoc"].tag, "{%s}element" %xmlnamespace.NS_XSD)
+        self.assertEqual(res["{vostok}CounterDoc"].get("name"), "CounterDoc")
+        self.assertEqual(res["{vostok}RobotDoc"].tag, "{%s}element" %xmlnamespace.NS_XSD)
+        self.assertEqual(res["{vostok}RobotDoc"].get("name"), "RobotDoc")
+        self.assertEqual(res["{vostok}ShishDoc"].tag, "{%s}element" %xmlnamespace.NS_XSD)
+        self.assertEqual(res["{vostok}ShishDoc"].get("name"), "ShishDoc")
 
     def test_convert(self):
         xtypes = self.schema.get_list_of_defined_types()
         self.assertTrue(hasattr(types["{sever}Car"], "weight"))
         self.assertEqual(types["{sever}Car"]._namespace, "sever")
         self.assertEqual(types["{sever}Car"].__class__.__name__, "ComplexTypeMeta")
+        # added after fixing issue 11 - present annotation in element tag
+        self.assertTrue("{vostok}PersonDoc" in types)
+        self.assertEqual(types["{vostok}PersonDoc"].__class__.__name__, "ComplexTypeMeta")
+        self.assertTrue(hasattr(types["{vostok}PersonDoc"], "age"))
+        self.assertTrue(hasattr(types["{vostok}PersonDoc"], "weight"))
+        self.assertTrue(hasattr(types["{vostok}PersonDoc"], "name"))
+        self.assertTrue(hasattr(types["{vostok}PersonDoc"], "place"))
+        self.assertTrue(hasattr(types["{vostok}PersonDoc"], "car"))
+        self.assertTrue(hasattr(types["{vostok}PersonDoc"], "Comment"))
+        self.assertEqual(types["{vostok}PersonDoc"]._namespace, "vostok")
+        self.assertTrue("{vostok}CounterDoc" in types)
+        self.assertEqual(types["{vostok}CounterDoc"].__base__.__name__, "XMLInteger")
+        self.assertTrue(isinstance(types["{vostok}CounterDoc"](), int))
+        self.assertEqual(types["{vostok}CounterDoc"]._namespace, "vostok")
+        self.assertTrue("{vostok}RobotDoc" in types)
+        self.assertEqual(types["{vostok}RobotDoc"].__class__.__name__, "ComplexTypeMeta")
+        self.assertTrue(hasattr(types["{vostok}RobotDoc"], "age"))
+        self.assertTrue(hasattr(types["{vostok}RobotDoc"], "weight"))
+        self.assertTrue(hasattr(types["{vostok}RobotDoc"], "name"))
+        self.assertTrue(hasattr(types["{vostok}RobotDoc"], "place"))
+        self.assertTrue(hasattr(types["{vostok}RobotDoc"], "car"))
+        self.assertTrue(hasattr(types["{vostok}RobotDoc"], "Comment"))
+        self.assertEqual(types["{vostok}RobotDoc"]._namespace, "vostok")
+        self.assertTrue("{vostok}ShishDoc" in types)
+        self.assertEqual(types["{vostok}ShishDoc"].__class__.__name__, "ComplexTypeMeta")
+        self.assertEqual(len(types["{vostok}ShishDoc"]._children), 0)