# HG changeset patch # User laurent # Date 1324072061 -3600 # Node ID 9c133d675b691a63c93de3f6aa3211a104a474ee # Parent d65122c61eaf6647f1fde8b0e2e515d77eafd9e7 Adding support for defining tag element without attributes or children as sequence element diff -r d65122c61eaf -r 9c133d675b69 xmlclass/xmlclass.py --- a/xmlclass/xmlclass.py Fri Dec 16 22:44:47 2011 +0100 +++ b/xmlclass/xmlclass.py Fri Dec 16 22:47:41 2011 +0100 @@ -557,16 +557,19 @@ "check": lambda x: isinstance(x, (StringType, UnicodeType, minidom.Node)) } -def GenerateTagInfos(name): +def GenerateTagInfos(infos): def ExtractTag(tree): if len(tree._attrs) > 0: - raise ValueError("\"%s\" musn't have attributes!" % name) + raise ValueError("\"%s\" musn't have attributes!" % infos["name"]) if len(tree.childNodes) > 0: - raise ValueError("\"%s\" musn't have children!" % name) - return None + raise ValueError("\"%s\" musn't have children!" % infos["name"]) + if infos["minOccurs"] == 0: + return True + else: + return None def GenerateTag(value, name=None, indent=0): - if name is not None: + if name is not None and not (infos["minOccurs"] == 0 and value is None): ind1, ind2 = getIndent(indent, name) return ind1 + "<%s/>\n" % name else: @@ -577,7 +580,7 @@ "extract": ExtractTag, "generate": GenerateTag, "initial": lambda: None, - "check": lambda x: x == None + "check": lambda x: x == None or infos["minOccurs"] == 0 and value == True } def FindTypeInfos(factory, infos): @@ -645,7 +648,7 @@ if element_infos is not None: sequence_element["elmt_type"] = element_infos elif choice["elmt_type"] == "tag": - choice["elmt_type"] = GenerateTagInfos(choice["name"]) + choice["elmt_type"] = GenerateTagInfos(choice) else: choice_infos = factory.ExtractTypeInfos(choice["name"], name, choice["elmt_type"]) if choice_infos is not None: @@ -1189,7 +1192,10 @@ infos = GenerateAnyInfos(element) else: elmtname = element["name"] - infos = self.ExtractTypeInfos(element["name"], name, element["elmt_type"]) + if element["elmt_type"] == "tag": + infos = GenerateTagInfos(element) + else: + infos = self.ExtractTypeInfos(element["name"], name, element["elmt_type"]) if infos is not None: element["elmt_type"] = infos if element["maxOccurs"] == "unbounded" or element["maxOccurs"] > 1: