# Writing output filters

The output of a Saxon stylesheet can be directed to a user-defined output filter. This filter can be defined either as a SAX2 `ContentHandler`, or as a subclass of the Saxon class `net.sf.saxon.event.Receiver`. One advantage of using an `Receiver` is that more information is available from the stylesheet, for example the attributes of the `xsl:output` element; another is that (if you are using the schema-aware version of the product) type annotations are available on element and attribute nodes.

Some `ContentHandler` implementations require a sequence of events corresponding to a well-formed document (that is, one whose document node has exactly one element node and no text nodes among its children). If this is the case, you can specify the additional output property `saxon:require-well-formed="yes"`, which will cause Saxon to report an error if the result tree is not well-formed.

As specified in the JAXP 1.1 interface, requests to disable or re-enable output escaping are also notified to the content handler by means of special processing instructions. The names of these processing instructions are defined by the constants PI_DISABLE_OUTPUT_ESCAPING and PI_ENABLE_OUTPUT_ESCAPING defined in class `javax.xml.transform.Result`.

The `Receiver` or `ContentHandler` to be used is specified in the `method` attribute of the `xsl:output` element, as a fully-qualified class name; for example `method="prefix:com.acme.xml.SaxonOutputFilter"`. The namespace prefix is ignored, but must be present to meet XSLT conformance rules.

See the documentation of class `net.sf.saxon.event.Receiver` for details of the methods available, or implementations such as HTMLEmitter and XMLEmitter and TEXTEmitter for the standard output formats supported by Saxon.

It can sometimes be useful to set up a chain of Receivers working as a pipeline. To write a filter that participates in such a pipeline, the class `ProxyReceiver` is supplied. Use the class `XMLIndenter`, which handles XML indentation, as an example of how to write a `ProxyReceiver`.

Rather than writing an output filter in Java, Saxon also allows you to process the output through another XSLT stylesheet. To do this, simply name the next stylesheet in the `saxon:next-in-chain` attribute of `xsl:output`.

Any number of user-defined attributes may be defined on `xsl:output`. These attributes must have names in a non-null namespace, which must not be either the XSLT or the Saxon namespace. These attributes are interpreted as attribute value templates. The value of the attribute is inserted into the `Properties` object made available to the `Emitter` handling the output; they will be ignored by the standard output methods, but can supply arbitrary information to a user-defined output method. The name of the property will be the expanded name of the attribute in JAXP format, for example `{http://my-namespace/uri}local-name`, and the value will be the value as given, after evaluation as an attribute value template.

Expand | Next