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 Matt Coarr <mc...@mitre.org> on 2004/01/06 17:14:38 UTC

problems using SAXSource instead of StreamSource

Hello,

I am having a problem when we changed our class to pass xalan a 
SAXSource object instead of a StreamSource object. 

When using a StreamSource everything worked properly as expected.  But 
when we switched over to SAXSource, none of the templates in the 
stylesheet are being applied.  The result is just the default template 
(copy text and apply templates to children) is applied.  So we just see 
the text of the original xml document all concatenated together.

Has anyone else run into this?

Here are the fragments...

Using a StreamSource (from TransformerResourceSource's process method):

> documentAsStream = getMethod.getResponseBodyAsStream();
> source = new StreamSource(documentAsStream);

                                                                         
And using SAXSource (from TransformerResourceSource's process method):

> documentAsStream = getMethod.getResponseBodyAsStream();
> InputSource inputSource = new InputSource(documentAsStream);
> SAXParserFactory factory = SAXParserFactory.newInstance();
> factory.setValidating(false);
> factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", 
> false);
> SAXParser parser = factory.newSAXParser();
> XMLReader xmlReader = parser.getXMLReader();
> source = new SAXSource(xmlReader, inputSource);


And to use either of these fragments:

> InputStream xslDocumentAsStream = null;
> GetMethod xslGetMethod = null;
> InputStream xmlDocumentAsStream = null;
> GetMethod xmlGetMethod = null;
>                                                                        
> // TransformerResourceSource using jakarta httpclient to fetch document
> // for data or stylesheet and allows source to be obtained via the
> // getSource method.  After the source is finished being used (e.g.
> // after the transformation has taken place), the cleanup method is 
> called.
>
> TransformerResourceSource xslResources = new TransformerResourceSource();
> xslResources.setUri(xslUri);
> xslResources.process();
>                                                                        
> TransformerResourceSource xmlResources = new TransformerResourceSource();
> xmlResources.setUri(xmlUri);
> xmlResources.process();
>                                                                        
> xslSource = xslResources.getSource();
> xmlSource = xmlResources.getSource();
>                                                                        
> outputResult = new StreamResult(output);
>                                                                        
> // create the transformer and transform
> tFactory = TransformerFactory.newInstance();
>     transformer = tFactory.newTransformer(xslSource);
>
> transformer.transform(xmlSource, outputResult);
>       
> xslResources.cleanup();
> xmlResources.cleanup();


Thank you,
Matt


Re: problems using SAXSource instead of StreamSource

Posted by Matt Coarr <mc...@mitre.org>.
Hi Elliotte,

That was exactly the problem. 

By using XMLReaderFactory instead of SAXParserFactory/SAXParser 
everything is working now.  I also switched the setFeature call to be on 
the XMLReader object now.

I think what I did was look at xercesImpl.jar and opened 
META-INF/services/javax.xml.parsers.SAXParserFactory, which said the 
class was org.apache.xerces.jaxp.SAXParserFactoryImpl.  So I worked from 
those interfaces and classes.

Thanks for pointing out the correct way of handling this.

Thanks,
Matt


Elliotte Rusty Harold wrote:

> You forgot to make your SAXParserFactory namespace aware, but don't 
> bother. Use XMLReaderFactory instead. SAXParserFactory is a designed 
> for SAX1. It does not work properly with SAX2, as your example shows.



Re: problems using SAXSource instead of StreamSource

Posted by Elliotte Rusty Harold <el...@metalab.unc.edu>.
At 11:14 AM -0500 1/6/04, Matt Coarr wrote:
>Hello,
>
>I am having a problem when we changed our class to pass xalan a 
>SAXSource object instead of a StreamSource object.
>When using a StreamSource everything worked properly as expected. 
>But when we switched over to SAXSource, none of the templates in the 
>stylesheet are being applied.  The result is just the default 
>template (copy text and apply templates to children) is applied.  So 
>we just see the text of the original xml document all concatenated 
>together.
>
>>  documentAsStream = getMethod.getResponseBodyAsStream();
>>  InputSource inputSource = new InputSource(documentAsStream);
>>  SAXParserFactory factory = SAXParserFactory.newInstance();
>>  factory.setValidating(false);
>

You forgot to make your SAXParserFactory namespace aware, but don't 
bother. Use XMLReaderFactory instead. SAXParserFactory is a designed 
for SAX1. It does not work properly with SAX2, as your example shows.
-- 

   Elliotte Rusty Harold
   elharo@metalab.unc.edu
   Effective XML (Addison-Wesley, 2003)
   http://www.cafeconleche.org/books/effectivexml
   http://www.amazon.com/exec/obidos/ISBN%3D0321150406/ref%3Dnosim/cafeaulaitA

Re: problems using SAXSource instead of StreamSource

Posted by Joseph Kesselman <ke...@us.ibm.com>.



Are you sure you're providing a SAX stream that meets our assumptions?
SAX's configurability sometimes causes problems in that regard...

We expect a namespace-aware SAX stream.

We expect namespace prefixes to be reported.

I believe we expect namespace nodes reported as attributes as well as via
start/end namespace declaration events.


(Making SAX2DTM more tolerant of other kinds of SAX stream is a
longstanding but relatively low-priority wishlist item. I'm not sure
whether it was ever posted into Bugzilla as an official Enhancement
request.)

______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"The world changed profoundly and unpredictably the day Tim Berners Lee
got bitten by a radioactive spider." -- Rafe Culpin, in r.m.filk