title: XML Transformation using DOMSource, DOMResult & XSLT
body: <p>I am trying to transform the following XML </p>
  
  <pre><code> &lt;PHONEBOOK&gt;
      &lt;PERSON&gt;
          &lt;NAME&gt;Ren1&lt;/NAME&gt;
          &lt;EMAIL&gt;ren1@gmail.com&lt;/EMAIL&gt;
          &lt;TELEPHONE&gt;999-999-9999&lt;/TELEPHONE&gt;
          &lt;WEB&gt;www.ren1.com&lt;/WEB&gt;
      &lt;/PERSON&gt;
      &lt;PERSON&gt;
          &lt;NAME&gt;Ren2&lt;/NAME&gt;
          &lt;EMAIL&gt;ren2@gmail.com&lt;/EMAIL&gt;
          &lt;TELEPHONE&gt;999-999-9999&lt;/TELEPHONE&gt;
          &lt;WEB&gt;www.ren2.com&lt;/WEB&gt;
      &lt;/PERSON&gt;
      &lt;PERSON&gt;
          &lt;NAME&gt;Ren3&lt;/NAME&gt;
          &lt;EMAIL&gt;ren3@gmail.com&lt;/EMAIL&gt;
          &lt;TELEPHONE&gt;999-999-9999&lt;/TELEPHONE&gt;
          &lt;WEB&gt;www.ren3.com&lt;/WEB&gt;
      &lt;/PERSON&gt;
  &lt;/PHONEBOOK&gt;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>&lt;Names&gt;&lt;Name&gt;Ren1&lt;/Name&gt;&lt;Name&gt;Ren2&lt;/Name&gt;&lt;Name&gt;Ren3&lt;/Name&gt;&lt;/Names&gt;
  </code></pre>
  
  <p>using DOMSource, DOMResult and XSLT.</p>
  
  <p>XSLT used is as follows</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0"&gt;
      &lt;xsl:output omit-xml-declaration="yes" method="xml"&gt;&lt;/xsl:output&gt;
  
      &lt;xsl:template match="/"&gt;
  
          &lt;Names&gt;
              &lt;xsl:for-each select="PHONEBOOK/PERSON"&gt;
                  &lt;Name&gt;
  
                      &lt;xsl:value-of select="NAME" /&gt;
                  &lt;/Name&gt;
              &lt;/xsl:for-each&gt;
          &lt;/Names&gt;
  </code></pre>
  
  <p>Java Code used for transformation:</p>
  
  <pre><code>package test1;
  
  import java.io.IOException;
  import java.io.StringWriter;
  import java.io.ObjectInputStream.GetField;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  import javax.xml.transform.OutputKeys;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.TransformerException;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.dom.DOMResult;
  import javax.xml.transform.dom.DOMSource;
  import javax.xml.transform.stream.StreamResult;
  import javax.xml.transform.stream.StreamSource;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Node;
  import org.xml.sax.SAXException;
  
  public class Test2 {
      public static void main(String[] args) throws TransformerException,
              ParserConfigurationException, SAXException, IOException {
          // TODO Auto-generated method stub
  
          //Stylesheet
          StreamSource stylesource = new StreamSource(
                  "src/test1/transform_stylesheet1.xsl");
  
          DocumentBuilderFactory docbFactory = DocumentBuilderFactory
                  .newInstance();
          DocumentBuilder dBuilder = docbFactory.newDocumentBuilder();
  
          //source XML
          Document sourceDoc = dBuilder.parse("src/test1/Sample1.xml");
  
          DOMSource source = new DOMSource(sourceDoc);
  
          TransformerFactory transformerFactory = TransformerFactory
                  .newInstance();
          Transformer transformer = transformerFactory
                  .newTransformer(stylesource);
  
          Document document = dBuilder.newDocument();
          DOMResult result = new DOMResult(document);
  
          transformer.transform(source, result);
  
          Node resultDoc = ((Document) result.getNode()).getDocumentElement();
  
          System.out.println(resultDoc.getChildNodes().getLength());
  
          // print the result
          StringWriter writer = new StringWriter();
          transformer.transform(new DOMSource(resultDoc), new StreamResult(writer));
          String str = writer.toString();
          System.out.println(str);
  
  
      }
  }
  </code></pre>
  
  <p>Output of the above is as follows:</p>
  
  <pre><code>3 &lt;Names/&gt;
  </code></pre>
  
  <p>but i expect,</p>
  
  <pre><code>3
  &lt;Names&gt;&lt;Name&gt;Ren1&lt;/Name&gt;&lt;Name&gt;Ren2&lt;/Name&gt;&lt;Name&gt;Ren3&lt;/Name&gt;&lt;/Names&gt;
  </code></pre>
  
  <p>i debugged the code and found that 'resultDoc' has the content which i expect. Am i missing something while printing the result?</p>