[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Transformer adds unwanted newlines


Subject: Re: [xsl] Transformer adds unwanted newlines
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Mon, 6 Oct 2008 10:01:49 +0100

2008/10/5 Michael Kay <mike@xxxxxxxxxxxx>:
>>
>> > What happens if you remove the (apparently unnecessary) second step?
>>
>> Umm, being a bear of very little brain, can you explain what
>> I need to change below to do this? Sorry to be so dense...
>>
>> >>   SAXParserFactory spf = SAXParserFactory.newInstance();
>> >>   spf.setNamespaceAware(true);
>> >>   SAXParser parser = spf.newSAXParser();
>> >>   reader = parser.getXMLReader();
>> >>   stf = (SAXTransformerFactory) TransformerFactory.newInstance();
>> >>   String s = serializeTree();
>> >>   System.out.println("DEBUG: " + s);     // <------- DEBUG
>> >>   InputSource input = new InputSource(new StringReader(s));
>> >>   filter = stf.newXMLFilter(new StreamSource(stylesheet));
>> >>   filter.setParent(reader);
>> >>   StreamResult result = new StreamResult(response.getWriter());
>> >>   Transformer transformer = stf.newTransformer();
>> >>   SAXSource transformSource = new SAXSource(filter, input);
>> >>   transformer.transform(transformSource, result);
>>
>
> Change it to
>
>>   SAXParserFactory spf = SAXParserFactory.newInstance();
>>   spf.setNamespaceAware(true);
>>   SAXParser parser = spf.newSAXParser();
>>   reader = parser.getXMLReader();
>>   stf = (SAXTransformerFactory) TransformerFactory.newInstance();
>>   String s = serializeTree();
>>   System.out.println("DEBUG: " + s);     // <------- DEBUG
>>   InputSource input = new InputSource(new StringReader(s));
>>   StreamResult result = new StreamResult(response.getWriter());
>>   Transformer transformer = stf.newTransformer(new
> StreamSource(stylesheet));
>>   SAXSource transformSource = new SAXSource(reader, input);
>>   transformer.transform(transformSource, result);

or you could just do (untested):

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
String s = serializeTree();
Result result = new StreamResult(response.getWriter());
transformer.transform(new SAXSource(new InputSource(new
StringReader(s))), result);

...as you don't need to use a SAXParser do you?

Also, depending on what serializeTree() does, you might not need to
serialize/reparse the input.


-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/


Current Thread