#!/usr/bin/env python3

from doctype import DocType
from xpath import *

xmldoc = '''<?xml version="1.0"?>
<animals>
  <chimp name="aap">
    <food>banaan</food>
  </chimp>
  <cat name="mies">
    <food>ansjovis</food>
  </cat>
  <chimp name="aapje">
    <food>apenootjes</food>
  </chimp>
</animals>
'''

doc = DocType(xmldoc)

# query: /animals/chimp/food/text()
#
#     SORT
#       COLLECT  'child' 'type' 'text' text
#         COLLECT  'child' 'name' 'node' food
#           COLLECT  'child' 'name' 'node' chimp
#             COLLECT  'child' 'name' 'node' animals
#               ROOT

result = doc.run(
    xPath(
        arg1 = dSort(
            arg1 = dCollect(
                ARG  = collect__child__type__text__text,
                arg1 = dCollect(
                    ARG  = collect__child__food,
                    arg1 = dCollect(
                        ARG  = collect__child__chimp,
                        arg1 = dCollect(
                            ARG  = collect__child__animals,
                            arg1 = dRoot()
                        )
                    )
                )
             )
        )
    )
)
print(result)
