title: How to recursive iterparse an LXML tree avoiding to enter twice a node?
body: <p>the recursive function is parseMML. I want it to parse a MathML expression into a Python one. The simple example mmlinput is por producing the fraction 3/5, but it produces:</p>
  
  <pre><code>['(', '(', '3', ')', '/', '(', '5', ')', '(', '3', ')', '(', '5', ')', ')']
  </code></pre>
  
  <p>Instead of:</p>
  
  <pre><code>['(', '(', '3', ')', '/', '(', '5', ')', ')']
  </code></pre>
  
  <p>Because I don't know how to get rid of the elements entered already recursively. Any ideas about how to skip them?</p>
  
  <p>Thanks</p>
  
  <pre><code>mmlinput='''&lt;?xml version="1.0"?&gt; &lt;math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/1998/Math/MathML http://www.w3.org/Math/XMLSchema/mathml2/mathml2.xsd"&gt; &lt;mrow&gt; &lt;mfrac&gt; &lt;mrow&gt; &lt;mn&gt;3&lt;/mn&gt; &lt;/mrow&gt; &lt;mrow&gt; &lt;mn&gt;5&lt;/mn&gt; &lt;/mrow&gt; &lt;/mfrac&gt; &lt;/mrow&gt; &lt;/math&gt;'''
  
  
  def parseMML(mmlinput):
      from lxml import etree
      from StringIO import *
      from lxml import objectify
      exppy=[]
      events = ("start", "end")
      context = etree.iterparse(StringIO(mmlinput),events=events)
      for action, elem in context:
          if (action=='start') and (elem.tag=='mrow'):
              exppy+='('
          if (action=='end') and (elem.tag=='mrow'):
              exppy+=')'
          if (action=='start') and (elem.tag=='mfrac'):
              mmlaux=etree.tostring(elem[0])
              exppy+=parseMML(mmlaux)
              exppy+='/'
              mmlaux=etree.tostring(elem[1])
              exppy+=parseMML(mmlaux)
          if action=='start' and elem.tag=='mn': #this is a number
              exppy+=elem.text
      return (exppy)
  </code></pre>