This very complex transformation rule needs at the very least extensive commenting and motivation - preferably showing some concrete examples. If this is too long it could be written in an accompanying markdown .md doc file