Adding external information to PaeseResults before return
===

I want to add external information to PaeseResults before return. I return the results of parsing as asXML(). The external data represented as dictionary so as to parsed as XML in the final parsing.

This the code before adding external data
    from pyparsing import *
 

    # a hypothetical outer parser, with an unparsed SkipTo element
    color = oneOf("red orange yellow green blue purple")
    expression = SkipTo("XXX") + Literal("XXX").setResultsName('ex') + color.setResultsName('color')
     
    data = "JUNK 100 200 10 XXX green"
    print expression.parseString(data).dump()
     
    # main grammar
    def minorgrammar(toks):
        # a simple inner grammar
        integer = Word(nums)
        grammar2 = integer("A").setResultsName('A') + integer("B").setResultsName('B') + integer("C").setResultsName('C')
        
        # use scanString to find the inner grammar
        # (since we just want the first occurrence, we can use next
        # instead of a for loop with a break)
        t,s,e = next(grammar2.scanString(toks[0],maxMatches=1))
        
       
        
        # remove 0'th element from toks
        del toks[0]
        
        # return a new ParseResults, the sum of t and everything 
        # in toks after toks[0] was removed
        return t + toks
     
        grammar1 = expression.setParseAction(minorgrammar)
    x = grammar1.parseString(data).asXML("main")
    print x 

-------------------
the output is

    <main>
      <A>100</A>
      <B>200</B>
      <C>10</C>
      <ex>XXX</ex>
      <color>green</color>
    </main>

-------------------
the code after adding external data

        ...
        external_data = {'name':'omar', 'age':'40'}
        
        return t + toks + ParseResults(external_data)
     
    grammar1 = expression.setParseAction(minorgrammar)
    
    x = grammar1.parseString(data).asXML("main")
    
    print x

the output
----------
 

    <main>
      <A>100</A>
      <B>200</B>
      <C>10</C>
      <ex>XXX</ex>
      <color>green</color>
      <ITEM>{&apos;age&apos;: &apos;40&apos;, &apos;name&apos;: &apos;omar&apos;}</ITEM>
    </main>

----------------
I want the output in the form

    <main>
      <A>100</A>
      <B>200</B>
      <C>10</C>
      <ex>XXX</ex>
      <color>green</color>
      <name>omar</name>
      <name>age</name>
    </main>


What is the error in that code ? Thans  


