You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Filipe Correia <fi...@paradigmaxis.pt> on 2002/05/25 12:03:18 UTC

XMLReader and SAX Filters

   Hi,

  I'm using the below code to create a transformation pipeline using SAX and filters.
The xsl stylesheet is in file style.xsl and xml data is received in the xmlStream String.
Some static functions that I use are part of a class called SAXProcessing (that I also
show below).
  I can't see what isn't working in this code. I was first using a XMLFilterImpl object
to generate some xml ( instead of the XMLReader ) and all was working fine. But when
 I solved to replace it with a XMLReader and parse the xml data from a xml data stream
 the problems started...
now the only output I get are the text nodes in my xml data. It seems that my xsl is
 simply being ignored.

   Any suggestions?


         Cheers,

              Filipe

--------------------------------------------------------------------------------------------------
	XMLReader xmlReader = null;

	try {  
		SAXParserFactory spfactory =
			SAXParserFactory.newInstance();

		spfactory.setValidating(false);

		SAXParser saxParser =
			spfactory.newSAXParser();

		xmlReader = saxParser.getXMLReader();

		XMLFilter xmlFilter =
			SAXProcessing.newXMLTransformerFilter("style.xsl");

		xmlFilter.setParent(xmlReader);

		TransformerHandler transHand =
			SAXProcessing.newTransformerHandlerSerializer( new StreamResult(System.out));

		xmlFilter.setContentHandler(transHand);

		xmlReader.parse( new InputSource( new StringReader(xmlStream)));

	} catch (Exception e) {
		...
	}
--------------------------------------------------------------------------------------------------

My SAXProcessing functions:

--------------------------------------------------------------------------------------------------
public static XMLFilter newXMLTransformerFilter(String xslFileName){
	try {
		SAXTransformerFactory saxTFactory =
			(SAXTransformerFactory) TransformerFactory.newInstance();

		return saxTFactory.newXMLFilter( new StreamSource(xslFileName)); 

	} catch (Exception e) {
		e.printStackTrace();
		System.exit(1);
	}
	return null;
}

private static TransformerHandler newTransformerHandlerSerializer(StreamResult result){
	try {
		SAXTransformerFactory saxTFactory =
			(SAXTransformerFactory) TransformerFactory.newInstance();
		TransformerHandler transHand = saxTFactory.newTransformerHandler(); 

		transHand.setResult( result );
		Transformer transformer = transHand.getTransformer();
		transformer.setOutputProperty(OutputKeys.METHOD, method);
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		transformer.setOutputProperty(
						OutputKeys.DOCTYPE_PUBLIC,
						"-//W3C//DTD HTML 4.01//EN");
		transformer.setOutputProperty(
						OutputKeys.DOCTYPE_SYSTEM,
						"http://www.w3.org/TR/html4/strict.dtd");

		transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
		transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
		return transHand;

	} catch (Exception e) {
		e.printStackTrace();
		System.exit(1);
	}
	return null;
}
--------------------------------------------------------------------------------------------------