Sax filter or XSLT transform
Oft times the question arises when to use SAX filters and when to use XSLT. Here Mike Kay offers help in deciding
Here's a starter for ten (sorry, that's a catchprase from a UK TV programme)
A SAX filter can sometimes be used instead of an XSLT transformation, and it can sometimes be used for pre-processing the input to an XSLT transformation, or for post-processing the output.
The main cases where a SAX filter can be useful are:
To solve problems of document size, you can:
A SAX filter can be used to transform the input data into a form that is more amenable to XSLT processing. Examples include:
A SAX filter can be used to capture features of the source document that are not representable in the XSLT data model. For example, entity references and CDATA sections, as well as DTD declarations, can all be captured in a SAX filter and translated into elements that are visible to the XSLT stylesheet.
A postprocessing SAX filter (or simply a SAX ContentHandler) is useful in two principal situations:
Sometimes a user-written serializer can be produced by subclassing the standard serializer supplied with your chosen product. This will of course be product-dependent and your code may not work with future releases of the product.
It's also possible to write a SAX filter to preprocess the stylesheet. This is less common, but it can be used to tackle problems such as dynamic sort keys, or XPath expressions that are contained within source documents.
The new STX specification provides the prospect of being able to write SAX filters without needing to do low-level Java coding. If this takes off, I think that the idea of doing a complex transformation as a pipeline of SAX filters, some generated using XSLT and some using STX, may become increasingly attractive. Although XSLT 2.0 deals with nearly all the limitations of XSLT 1.0 in areas such as text processing, grouping, and aggregation, it doesn't address the problem of handling large input documents.
Streaming Transformations for XML (STX) is a one-pass transformation language for XML documents that builds on the Simple API for XML (SAX). STX is intended as a high-speed, low memory consumption alternative to XSLT. Since it does not require the construction of an in-memory tree, it is suitable for use in resource constrained scenarios. The aim of this project is to develop and maintain STX language specification.
Attention good readers. I'm looking for two examples.
1. sax filter feeding an XSLT transform
2. XSLT transform feeding into a Sax filter.
Could you help?