I am trying to write a java application to chain together xslt
stylesheets. My code below is taken out of Michael Kay's book. I almost
have gotten it to run except the very last part.
What I need to do is chain serveral stylesheets together, and then run
the final transfromation through a SAX filter to change the encoding of
the file. (I need to change unicode to microsoft ascii: Ö =>
/'u214/'d6)
The first two stylesheets transform the document. But the last SAX
filter has no affect. I know that the document is running through the
SAX filter because if I tell the SAX class to simply print out the name
of the element, it does. But the SAX class does not filter to my final
output.
Oddly enough, if I had an extra stylesheet *after* the SAX filter, then
the SAX filter does take affect.
I am probably overlooking something silly.
Thanks
Paul
*******************************************************
//run program which will have a pipe
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.sax.*;
import org.xml.sax.XMLFilter;
import java.io.File;
import org.xml.sax.helpers.XMLFilterImpl;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
class Pipeline{
public static void main(String[] args) throws Exception{
int len = args.length;
if (len != 1){
System.err.println("Please provide one argument");
System.exit(1);
}
new Pipeline().run(args[0]);
}
public void run(String input) throws Exception{
StreamSource source = new StreamSource(new File(input));
File style = new File("/home/paul/paultemp/test.xsl");
File style2 = new File("/home/paul/paultemp/test2.xsl");
File style3 = new File("/home/paul/paultemp/test3.xsl");
TransformerFactory factory = TransformerFactory.newInstance();
if (!factory.getFeature(SAXTransformerFactory.FEATURE_XMLFILTER)){
System.err.println("SAX Filters are not supported");
}
else{
System.err.println("Okay, we're ready to go!");
SAXTransformerFactory saxFactory = (SAXTransformerFactory)factory;
//first filter
XMLFilter filter = saxFactory.newXMLFilter(new StreamSource(style));
filter.setParent(new com.icl.saxon.aelfred.SAXDriver());
//second filter
XMLFilter filter2 = saxFactory.newXMLFilter(new
StreamSource(style2));
filter2.setParent(filter);
//third filter
XMLFilter filter3 = new PostFilter();
filter3.setParent(filter2);
//If I used a fourth filter and change the last two lines in this
method to
//"filter4", then the above SAX filter works!
//fourth filter
//XMLFilter filter4 = saxFactory.newXMLFilter(new
StreamSource(style3));
//filter4.setParent(filter3);
TransformerHandler serializer = saxFactory.newTransformerHandler();
serializer.setResult(new StreamResult (System.out));
Transformer trans = serializer.getTransformer();
filter3.setContentHandler(serializer);
filter3.parse(source.getSystemId());
}
}
}
//This is just temporary. I need to change all the character info
// to a funky encoding.
class PostFilter extends XMLFilterImpl{
public void startElement(String uri, String localName, String qName,
Attributes atts)
throws SAXException{
String newLocalName = localName.toUpperCase();
//if the line below is uncommented, the new local name appears
//System.err.println(newLocalName);
super.startElement(uri, newLocalName, qName, atts);
}
}
--
************************
*Paul Tremblay *
*phthenry(_at_)earthlink(_dot_)net*
************************
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list