xmlclass/xmlclass.py
changeset 1305 714f1381a09a
parent 1304 6be6c1e0e4d0
child 1315 ff14a66bbd12
equal deleted inserted replaced
1304:6be6c1e0e4d0 1305:714f1381a09a
   664             if choices_dict.has_key(choice_name):
   664             if choices_dict.has_key(choice_name):
   665                 raise ValueError("'%s' element defined two times in choice" % choice_name)
   665                 raise ValueError("'%s' element defined two times in choice" % choice_name)
   666             choices_dict[choice_name] = infos
   666             choices_dict[choice_name] = infos
   667     choices_xpath = "|".join(map(lambda x: "%s:%s" % (factory.TargetNamespace, x), choices_dict.keys()))
   667     choices_xpath = "|".join(map(lambda x: "%s:%s" % (factory.TargetNamespace, x), choices_dict.keys()))
   668     
   668     
   669     def GetContentChoicesXPath():
       
   670         return choices_xpath
       
   671     
       
   672     def GetContentInitial():
   669     def GetContentInitial():
   673         content_name, infos = choices[0]
   670         content_name, infos = choices[0]
   674         if content_name == "sequence":
   671         if content_name == "sequence":
   675             content_value = []
   672             content_value = []
   676             for i in xrange(infos["minOccurs"]):
   673             for i in xrange(infos["minOccurs"]):
   677                 for element_infos in infos["elements"]:
   674                 for element_infos in infos["elements"]:
   678                     value = GetElementInitialValue(factory, element_infos)
   675                     content_value.extend(GetElementInitialValue(factory, element_infos))
   679                     if value is not None:
       
   680                         if element_infos["type"] == CHOICE:
       
   681                             content_value.append(value)
       
   682                         else:
       
   683                             content_value.append({"name": element_infos["name"], "value": value})
       
   684         else:
   676         else:
   685             content_value = GetElementInitialValue(factory, infos)
   677             content_value = GetElementInitialValue(factory, infos)
   686         return {"name": content_name, "value": content_value}
   678         return content_value
   687         
   679         
   688     return {
   680     return {
   689         "type": COMPLEXTYPE,
   681         "type": COMPLEXTYPE,
   690         "choices_xpath": GetContentChoicesXPath,
   682         "choices_xpath": etree.XPath(choices_xpath, namespaces=factory.NSMAP),
   691         "initial": GetContentInitial,
   683         "initial": GetContentInitial,
   692     }
   684     }
   693 
   685 
   694 #-------------------------------------------------------------------------------
   686 #-------------------------------------------------------------------------------
   695 #                           Structure extraction functions
   687 #                           Structure extraction functions
  1210         
  1202         
  1211         elif elements.has_key(name):
  1203         elif elements.has_key(name):
  1212             element_infos = elements[name]
  1204             element_infos = elements[name]
  1213             element_infos["elmt_type"] = FindTypeInfos(factory, element_infos["elmt_type"])
  1205             element_infos["elmt_type"] = FindTypeInfos(factory, element_infos["elmt_type"])
  1214             if element_infos["type"] == CHOICE:
  1206             if element_infos["type"] == CHOICE:
  1215                 content = self.xpath(element_infos["elmt_type"]["choices_xpath"](), namespaces=factory.NSMAP)
  1207                 content = element_infos["elmt_type"]["choices_xpath"](self)
  1216                 if element_infos["maxOccurs"] == "unbounded" or element_infos["maxOccurs"] > 1:
  1208                 if element_infos["maxOccurs"] == "unbounded" or element_infos["maxOccurs"] > 1:
  1217                     return content
  1209                     return content
  1218                 elif len(content) > 0:
  1210                 elif len(content) > 0:
  1219                     return content[0]
  1211                     return content[0]
  1220                 return None 
  1212                 return None 
  1259                 element_infos["elmt_type"]["generate"](self, value)
  1251                 element_infos["elmt_type"]["generate"](self, value)
  1260             
  1252             
  1261             else:
  1253             else:
  1262                 element_xpath = ("%s:%s" % (factory.TargetNamespace, name)
  1254                 element_xpath = ("%s:%s" % (factory.TargetNamespace, name)
  1263                                  if name != "content"
  1255                                  if name != "content"
  1264                                  else elements["content"]["elmt_type"]["choices_xpath"]())
  1256                                  else elements["content"]["elmt_type"]["choices_xpath"].path)
  1265                 
  1257                 
  1266                 for element in self.xpath(element_xpath, namespaces=factory.NSMAP):
  1258                 for element in self.xpath(element_xpath, namespaces=factory.NSMAP):
  1267                     self.remove(element)
  1259                     self.remove(element)
  1268                 
  1260                 
  1269                 if value is not None:
  1261                 if value is not None:
  1270                     element_idx = elements.keys().index(name)
  1262                     element_idx = elements.keys().index(name)
  1271                     if element_idx > 0:
  1263                     if element_idx > 0:
  1272                         previous_elements_xpath = "|".join(map(
  1264                         previous_elements_xpath = "|".join(map(
  1273                             lambda x: "%s:%s" % (factory.TargetNamespace, x)
  1265                             lambda x: "%s:%s" % (factory.TargetNamespace, x)
  1274                                       if x != "content"
  1266                                       if x != "content"
  1275                                       else elements["content"]["elmt_type"]["choices_xpath"](),
  1267                                       else elements["content"]["elmt_type"]["choices_xpath"].path,
  1276                             elements.keys()[:element_idx]))
  1268                             elements.keys()[:element_idx]))
  1277                         
  1269                         
  1278                         insertion_point = len(self.xpath(previous_elements_xpath, namespaces=factory.NSMAP))
  1270                         insertion_point = len(self.xpath(previous_elements_xpath, namespaces=factory.NSMAP))
  1279                     else:
  1271                     else:
  1280                         insertion_point = 0
  1272                         insertion_point = 0