xmlclass/xsdschema.py
changeset 1290 13ee5f4ab612
parent 1078 f0ea86d830ed
child 1315 ff14a66bbd12
equal deleted inserted replaced
1286:adda406d3960 1290:13ee5f4ab612
   937     factory.AttributeFormDefault = attributes["attributeFormDefault"]
   937     factory.AttributeFormDefault = attributes["attributeFormDefault"]
   938     factory.ElementFormDefault = attributes["elementFormDefault"]
   938     factory.ElementFormDefault = attributes["elementFormDefault"]
   939     factory.BlockDefault = attributes["blockDefault"]
   939     factory.BlockDefault = attributes["blockDefault"]
   940     factory.FinalDefault = attributes["finalDefault"]
   940     factory.FinalDefault = attributes["finalDefault"]
   941     
   941     
   942     if attributes.has_key("targetNamespace"):
   942     factory.TargetNamespace = factory.DefinedNamespaces.get(attributes["targetNamespace"], None)
   943         factory.TargetNamespace = factory.DefinedNamespaces.get(attributes["targetNamespace"], None)
   943     if factory.TargetNamespace is not None:
       
   944         factory.etreeNamespaceFormat = "{%s}%%s" % attributes["targetNamespace"]
   944     factory.Namespaces[factory.TargetNamespace] = {}
   945     factory.Namespaces[factory.TargetNamespace] = {}
   945     
   946     
   946     annotations, children = factory.ReduceElements(elements, True)
   947     annotations, children = factory.ReduceElements(elements, True)
   947     
   948     
   948     for child in children:
   949     for child in children:
  1028         for child in self.Document.childNodes:
  1029         for child in self.Document.childNodes:
  1029             if child.nodeType == self.Document.ELEMENT_NODE:
  1030             if child.nodeType == self.Document.ELEMENT_NODE:
  1030                 schema = child
  1031                 schema = child
  1031                 break
  1032                 break
  1032         for qualified_name, attr in schema._attrs.items():
  1033         for qualified_name, attr in schema._attrs.items():
  1033             value = GetAttributeValue(attr)
  1034             namespace, name = DecomposeQualifiedName(qualified_name)
  1034             if value == "http://www.w3.org/2001/XMLSchema":
  1035             if namespace == "xmlns":
  1035                 namespace, name = DecomposeQualifiedName(qualified_name)
  1036                 value = GetAttributeValue(attr)
  1036                 if namespace == "xmlns":
  1037                 self.DefinedNamespaces[value] = name
  1037                     self.DefinedNamespaces["http://www.w3.org/2001/XMLSchema"] = name
  1038                 self.NSMAP[name] = value
       
  1039                 if value == "http://www.w3.org/2001/XMLSchema":
  1038                     self.SchemaNamespace = name
  1040                     self.SchemaNamespace = name
  1039                 else:
  1041                     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)
  1042         self.Schema = XSD_NAMESPACE["schema"]["extract"]["default"](self, schema)
  1043         ReduceSchema(self, self.Schema[1], self.Schema[2])
  1043         ReduceSchema(self, self.Schema[1], self.Schema[2])
  1044 
  1044 
  1045     def FindSchemaElement(self, element_name, element_type=None):
  1045     def FindSchemaElement(self, element_name, element_type=None):
  1046         namespace, name = DecomposeQualifiedName(element_name)
  1046         namespace, name = DecomposeQualifiedName(element_name)
  1082                     self.Namespaces[self.TargetNamespace][element_name] = element_infos
  1082                     self.Namespaces[self.TargetNamespace][element_name] = element_infos
  1083                     return element_infos
  1083                     return element_infos
  1084         return None
  1084         return None
  1085 
  1085 
  1086 """
  1086 """
  1087 This function opens the xsd file and generate the classes from the xml tree
  1087 This function opens the xsd file and generate a xml parser with class lookup from 
       
  1088 the xml tree
  1088 """
  1089 """
  1089 def GenerateClassesFromXSD(filepath):
  1090 def GenerateParserFromXSD(filepath):
  1090     xsdfile = open(filepath, 'r')
  1091     xsdfile = open(filepath, 'r')
  1091     factory = XSDClassFactory(minidom.parse(xsdfile), filepath)
  1092     xsdstring = xsdfile.read()
  1092     xsdfile.close()
  1093     xsdfile.close()
  1093     return GenerateClasses(factory)
  1094     return GenerateParser(XSDClassFactory(minidom.parseString(xsdstring), filepath), xsdstring)
  1094 
  1095 
  1095 """
  1096 """
  1096 This function generate the classes from the xsd given as a string
  1097 This function generate a xml from the xsd given as a string
  1097 """
  1098 """
  1098 def GenerateClassesFromXSDstring(xsdstring):
  1099 def GenerateParserFromXSDstring(xsdstring):
  1099     return GenerateClasses(XSDClassFactory(minidom.parseString(xsdstring)))
  1100     return GenerateParser(XSDClassFactory(minidom.parseString(xsdstring)))
  1100 
  1101 
  1101 
  1102 
  1102 #-------------------------------------------------------------------------------
  1103 #-------------------------------------------------------------------------------
  1103 #                           XSD schema syntax elements
  1104 #                           XSD schema syntax elements
  1104 #-------------------------------------------------------------------------------
  1105 #-------------------------------------------------------------------------------