in reply to Parsing a tree instead of a string?

XML::Twig has a method that might implement a subset of what you are looking for: wrap_children. Look for it in the code and in the docs. It lets you give a regexp on the children of an element, for exemple $doc->wrap_children( '<title><para>*', 'section') will wrap a title followed by para's in a section element.

At least it will give you one possible way of solving your problem: serialize the tree (in the case of wrap_children not all the tree is serialized, just the start tags of children), and then buid a regular expression that you can apply to that serialized tree. This way you don't have to re-invent the regexp engine wheel, which frankly would scare me!