/* The identity transformation */ Copy = _[/Copy]* /* Keep only the root tag of the first element */ Root = _[] /* Keep only the first element */ Head = _[/Copy] /* Remove the first element */ Tail = !Copy /* Reverse the forest */ Reverse = !Reverse,Head /* Keep only the leaves (empty elements) */ Any = _[Any]* Leaves = < _[],Any : _[] : /Leaves >* /* Flatten the structure */ Flatten = (_[], /Flatten)* /*************************/ txt = (h[ title[], txt* ] | p[])* toc = ul[ li[ title[], toc ]* ] html = ul[ li[ title[], html ]+ ]? MkToc = ul[ < h[Any] : li[/(Head,!MkToc)] : () >* ] Fix = < ul[] : () : _[/Fix] >* #check MkToc: txt -> toc #check Fix: toc -> html /*** Contributed by Akihiko Tozawa **********/ input = doc[preface, (div|p|note)*] div = div[(div|p|note)*] preface = preface[header, p[]*] note = note[p*] p = p[] header = header[] output = doc[(div'|p)*,appendix] div'= div[(div'|p)*] appendix = appendix[header,p*] Body = Rest = Appendix = doc[/(Body*),appendix[/(Rest*)]] Empty = _[Empty] #run Appendix(doc[preface[header[],p[]],p[],div[note[p[],p[]],p[]]]) #check Appendix : input -> output