xmlclass/xsdschema.py
changeset 1329 9d0cb01312f0
parent 1322 0a9227f743b3
child 1373 4278d5c1e414
equal deleted inserted replaced
1288:adc79fc44079 1329:9d0cb01312f0
   922     if factory.TargetNamespace == include_factory.TargetNamespace:
   922     if factory.TargetNamespace == include_factory.TargetNamespace:
   923         factory.Namespaces[factory.TargetNamespace].update(include_factory.Namespaces[include_factory.TargetNamespace])
   923         factory.Namespaces[factory.TargetNamespace].update(include_factory.Namespaces[include_factory.TargetNamespace])
   924     else:
   924     else:
   925         factory.Namespaces[include_factory.TargetNamespace] = include_factory.Namespaces[include_factory.TargetNamespace]
   925         factory.Namespaces[include_factory.TargetNamespace] = include_factory.Namespaces[include_factory.TargetNamespace]
   926     factory.ComputedClasses.update(include_factory.ComputedClasses)
   926     factory.ComputedClasses.update(include_factory.ComputedClasses)
       
   927     factory.ComputedClassesLookUp.update(include_factory.ComputedClassesLookUp)
       
   928     factory.EquivalentClassesParent.update(include_factory.EquivalentClassesParent)
   927     return None
   929     return None
   928     
   930     
   929 def ReduceRedefine(factory, attributes, elements):
   931 def ReduceRedefine(factory, attributes, elements):
   930     annotations, children = factory.ReduceElements(elements)
   932     annotations, children = factory.ReduceElements(elements)
   931     raise ValueError("\"redefine\" element isn't supported yet!")
   933     raise ValueError("\"redefine\" element isn't supported yet!")
   937     factory.AttributeFormDefault = attributes["attributeFormDefault"]
   939     factory.AttributeFormDefault = attributes["attributeFormDefault"]
   938     factory.ElementFormDefault = attributes["elementFormDefault"]
   940     factory.ElementFormDefault = attributes["elementFormDefault"]
   939     factory.BlockDefault = attributes["blockDefault"]
   941     factory.BlockDefault = attributes["blockDefault"]
   940     factory.FinalDefault = attributes["finalDefault"]
   942     factory.FinalDefault = attributes["finalDefault"]
   941     
   943     
   942     if attributes.has_key("targetNamespace"):
   944     targetNamespace = attributes.get("targetNamespace", None)
   943         factory.TargetNamespace = factory.DefinedNamespaces.get(attributes["targetNamespace"], None)
   945     factory.TargetNamespace = factory.DefinedNamespaces.get(targetNamespace, None)
       
   946     if factory.TargetNamespace is not None:
       
   947         factory.etreeNamespaceFormat = "{%s}%%s" % targetNamespace
   944     factory.Namespaces[factory.TargetNamespace] = {}
   948     factory.Namespaces[factory.TargetNamespace] = {}
   945     
   949     
   946     annotations, children = factory.ReduceElements(elements, True)
   950     annotations, children = factory.ReduceElements(elements, True)
   947     
   951     
   948     for child in children:
   952     for child in children:
  1028         for child in self.Document.childNodes:
  1032         for child in self.Document.childNodes:
  1029             if child.nodeType == self.Document.ELEMENT_NODE:
  1033             if child.nodeType == self.Document.ELEMENT_NODE:
  1030                 schema = child
  1034                 schema = child
  1031                 break
  1035                 break
  1032         for qualified_name, attr in schema._attrs.items():
  1036         for qualified_name, attr in schema._attrs.items():
  1033             value = GetAttributeValue(attr)
  1037             namespace, name = DecomposeQualifiedName(qualified_name)
  1034             if value == "http://www.w3.org/2001/XMLSchema":
  1038             if namespace == "xmlns":
  1035                 namespace, name = DecomposeQualifiedName(qualified_name)
  1039                 value = GetAttributeValue(attr)
  1036                 if namespace == "xmlns":
  1040                 self.DefinedNamespaces[value] = name
  1037                     self.DefinedNamespaces["http://www.w3.org/2001/XMLSchema"] = name
  1041                 self.NSMAP[name] = value
       
  1042                 if value == "http://www.w3.org/2001/XMLSchema":
  1038                     self.SchemaNamespace = name
  1043                     self.SchemaNamespace = name
  1039                 else:
  1044                     self.Namespaces[self.SchemaNamespace] = XSD_NAMESPACE
  1040                     self.DefinedNamespaces["http://www.w3.org/2001/XMLSchema"] = self.SchemaNamespace
       
  1041                 self.Namespaces[self.SchemaNamespace] = XSD_NAMESPACE
       
  1042         self.Schema = XSD_NAMESPACE["schema"]["extract"]["default"](self, schema)
  1045         self.Schema = XSD_NAMESPACE["schema"]["extract"]["default"](self, schema)
  1043         ReduceSchema(self, self.Schema[1], self.Schema[2])
  1046         ReduceSchema(self, self.Schema[1], self.Schema[2])
  1044 
  1047 
  1045     def FindSchemaElement(self, element_name, element_type=None):
  1048     def FindSchemaElement(self, element_name, element_type=None):
  1046         namespace, name = DecomposeQualifiedName(element_name)
  1049         namespace, name = DecomposeQualifiedName(element_name)
  1082                     self.Namespaces[self.TargetNamespace][element_name] = element_infos
  1085                     self.Namespaces[self.TargetNamespace][element_name] = element_infos
  1083                     return element_infos
  1086                     return element_infos
  1084         return None
  1087         return None
  1085 
  1088 
  1086 """
  1089 """
  1087 This function opens the xsd file and generate the classes from the xml tree
  1090 This function opens the xsd file and generate a xml parser with class lookup from 
       
  1091 the xml tree
  1088 """
  1092 """
  1089 def GenerateClassesFromXSD(filepath):
  1093 def GenerateParserFromXSD(filepath):
  1090     xsdfile = open(filepath, 'r')
  1094     xsdfile = open(filepath, 'r')
  1091     factory = XSDClassFactory(minidom.parse(xsdfile), filepath)
  1095     xsdstring = xsdfile.read()
  1092     xsdfile.close()
  1096     xsdfile.close()
  1093     return GenerateClasses(factory)
  1097     cwd = os.getcwd()
       
  1098     os.chdir(os.path.dirname(filepath))
       
  1099     parser = GenerateParser(XSDClassFactory(minidom.parseString(xsdstring), filepath), xsdstring)
       
  1100     os.chdir(cwd)
       
  1101     return parser
  1094 
  1102 
  1095 """
  1103 """
  1096 This function generate the classes from the xsd given as a string
  1104 This function generate a xml from the xsd given as a string
  1097 """
  1105 """
  1098 def GenerateClassesFromXSDstring(xsdstring):
  1106 def GenerateParserFromXSDstring(xsdstring):
  1099     return GenerateClasses(XSDClassFactory(minidom.parseString(xsdstring)))
  1107     return GenerateParser(XSDClassFactory(minidom.parseString(xsdstring)), xsdstring)
  1100 
  1108 
  1101 
  1109 
  1102 #-------------------------------------------------------------------------------
  1110 #-------------------------------------------------------------------------------
  1103 #                           XSD schema syntax elements
  1111 #                           XSD schema syntax elements
  1104 #-------------------------------------------------------------------------------
  1112 #-------------------------------------------------------------------------------
  2518 
  2526 
  2519     # Complex Types
  2527     # Complex Types
  2520     "anyType": {"type": COMPLEXTYPE, "extract": lambda x:None},
  2528     "anyType": {"type": COMPLEXTYPE, "extract": lambda x:None},
  2521 }
  2529 }
  2522 
  2530 
  2523 if __name__ == '__main__':
       
  2524     classes = GenerateClassesFromXSD("test.xsd")
       
  2525     
       
  2526     # Code for test of test.xsd
       
  2527     xmlfile = open("po.xml", 'r')
       
  2528     tree = minidom.parse(xmlfile)
       
  2529     xmlfile.close()
       
  2530     test = classes["PurchaseOrderType"]()
       
  2531     for child in tree.childNodes:
       
  2532         if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "purchaseOrder":
       
  2533             test.loadXMLTree(child)
       
  2534     test.items.item[0].setquantity(2)
       
  2535     testfile = open("test.xml", 'w')
       
  2536     testfile.write(u'<?xml version=\"1.0\"?>\n')
       
  2537     testfile.write(test.generateXMLText("purchaseOrder").encode("utf-8"))
       
  2538     testfile.close()