Reading this, I am increasing my belief that this is very, if not exactly, like XSLT. You are searching a tree looking for parents, siblings, children, etc nodes then analysing or transforming them.
https://libcst.readthedocs.io/en/latest/tutorial.html#Build-...
It is a little less pure than the functional XSLT, and also a lot more practical.