1. A method of transforming a document, including: receiving events in a stream, including a plurality of element start-events, input events and element end-events; responding to an element start-event for an element by matching the element to 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 a matching named template, 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.