Treat the same XML tag differently depending on the parent with etree/python?

wilah

New Member
I'm working with a python script to parse XML and create a python file from it.At the top level the XML looks like;\[code\]<stage id="stage1"> <initialise> </initialise> <execute> </execute></stage>\[/code\]Here is a sample of the full XML. Quite complicated stuff.I've got a parser written to handle most things, but in this instance, is it possible to check up the tree for the \[code\]</initialise>\[/code\] coming before the \[code\]<execute>\[/code\] in order for that to identify differently.Can you do this using the \[code\]elem[x]\[/code\] type method or maybe XPath?\[code\]<initialise> <variable id="maxLimit" value="http://stackoverflow.com/questions/10414709/0" type="Integer"> <validate> <regularExpression></regularExpression> </validate> <execute> </execute> </variable></initialise><execute> # treat this differently\[/code\]The Python that grabs the execute elements looks like this;\[code\]def parse_execute(self, elem): if elem[0].tag == 'required': #or elem[0].tag == 'variable': self.loop(elem) else: self.out('') self.out('def {0}():'.format(elem.tag)) self.indent() self.loop(elem) self.dedent()\[/code\]
 
Back
Top