1. A computer-implemented method of transforming a document, including: receiving events in a stream generated by parsing of an input file using a computer, including a plurality of element start-events, input events and element end-events; responding to an element start-event for an element by matching an element name for the element to a template name for a named template and activating the named template or, lacking a match, by treating the element as content of a parent element; responding to an element end-event for a current element using the named template that was activated in response to the element start-event for the current element, if any, including: responsive to instructions in the matching named template, outputting at least some of literal text or input events corresponding to the current element; clearing the input events corresponding to content of the current element; and deactivating the matching named template.