File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes xslt transformation with DOMSource and Streamsource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xslt transformation with DOMSource and Streamsource" Watch "xslt transformation with DOMSource and Streamsource" New topic

xslt transformation with DOMSource and Streamsource

Anand Sidharth
Ranch Hand

Joined: Dec 17, 2003
Posts: 44
When a node being used in the tranformation is empty the DOMSource and Stream
source produce different results. For example -
Input Xml -
<SampleData attr1="" attr2="b" attr3="c" />
<xsl:stylesheet xmlns:xsl="" version="1.0" >
<xsl utput method="xml" indent="yes" xalan:indent-amount="4"
<xsl:template match="SampleData">
<attr1><xsl:value-of select="@attr1" /></attr1>
<attr2><xsl:value-of select="@attr2" /></attr2>
<attr3><xsl:value-of select="@attr3" /></attr3>
Transformer class -
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class SampleTransform
public static void main(String[] args) throws Exception
String sampleFileName="sample-input-data.xml";
TransformerFactory tFactory = TransformerFactory.newInstance();
DocumentBuilder builder = DocumentBuilderFactory.newInstance
InputSource inputSource = new InputSource(new FileReader(sampleFileName));
Document document = builder.parse(inputSource);
DOMSource domSource = new DOMSource();
// Create the transformer
Transformer transformer = tFactory.newTransformer(new StreamSource

// transform the input xml using stream source
transformer.transform(new StreamSource(sampleFileName),
new StreamResult(new FileOutputStream("output-using-stream.out")));
// transform the input xml using dom source
transformer.transform(domSource, new StreamResult(new FileOutputStream
System.out.println("check output files output-dom.out and output-

The output using the stream source is -
<?xml version="1.0" encoding="UTF-8"?>
Output using the dom source is
<?xml version="1.0" encoding="UTF-8"?>
Please note the extra new line character in the "attr1" element. The dom source
and stream source should produce the same output. However, in the case shown
about, this does not happen.
Can anybody tell why this occurs???
I agree. Here's the link:
subject: xslt transformation with DOMSource and Streamsource
It's not a secret anymore!