title: Formatting the output of XML transformation using XSLT to generate the Text file
body: <p>I just started using <code>XSLT</code> for transforming an <code>XML</code> to output as <code>test</code> file.</p>
  
  <p>Here is my XML input file:</p>
  
  <pre><code>&lt;?xml version="1.0" ?&gt;
  &lt;transcript&gt;
      &lt;student id="STU12345" name="name 1" status="active"&gt;
          &lt;home_address&gt;35 Wall Street, Wonderland, NJ&lt;/home_address&gt;
          &lt;interests&gt;
              &lt;interest&gt;interest 1&lt;/interest&gt;
              &lt;interest&gt;interest 2&lt;/interest&gt;
              &lt;interest&gt;interest 3&lt;/interest&gt;
          &lt;/interests&gt;
      &lt;/student&gt;
      &lt;term&gt;
          &lt;heading name="Winter 1999" /&gt;
          &lt;course&gt;
              &lt;course-name&gt;course 1&lt;/course-name&gt;
              &lt;grade&gt;A-&lt;/grade&gt;
              &lt;credits&gt;4&lt;/credits&gt;
          &lt;/course&gt;
          &lt;course&gt;
              &lt;course-name&gt;course 2&lt;/course-name&gt;
              &lt;grade&gt;B+&lt;/grade&gt;
              &lt;credits&gt;3&lt;/credits&gt;
          &lt;/course&gt;
      &lt;/term&gt;
      &lt;summary&gt;summary&lt;/summary&gt;
      &lt;comments&gt;
          comments
      &lt;/comments&gt;
  &lt;/transcript&gt;
  </code></pre>
  
  <p>My XML file to transform this xml is:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output method="text" indent="no" /&gt;
  
      &lt;xsl:template match="transcript"&gt;
          Student Transcript
          &lt;xsl:apply-templates select="student" /&gt;
  
  Course Name | Grade | Credits
          &lt;xsl:apply-templates select="term" /&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="student"&gt;
          Name: &lt;xsl:value-of select="@name" /&gt;
          ID: &lt;xsl:value-of select="@id" /&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="term"&gt;&lt;xsl:apply-templates /&gt;&lt;/xsl:template&gt;
  
      &lt;xsl:template match="course"&gt;&lt;xsl:value-of select="course-name"/&gt; |&lt;xsl:value-of select="grade" /&gt; | &lt;xsl:value-of select="credits" /&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The output that I am getting now is:</p>
  
  <pre><code>        Student Transcript
  
          Name: name 1
          ID: STU12345
  
  Course Name | Grade | Credits
  
  
          course 1 |A- | 4
          course 2 |B+ | 3
  </code></pre>
  
  <p>But I am trying to get the output as:</p>
  
  <pre><code>        Student Transcript
  
          Name: name 1
          ID: STU12345
  
  Course Name | Grade | Credits
  course 1    |A-     | 4
  course 2    |B+     | 3
  </code></pre>
  
  <p>Finally my Java program that helps me to do the transformation:</p>
  
  <pre><code>public static void main(String[] args) {
      String xml = "input.xml";
      String xslt = "input.xsl";
      String output = "output.txt";
      try {
          TransformerFactory tf = TransformerFactory.newInstance();
          Transformer tr = tf.newTransformer(new StreamSource(xslt));
          tr.transform(new StreamSource(xml), new StreamResult(
                  new FileOutputStream(output)));
  
          System.out.println("Output to " + output);
      } catch (Exception e) {
          System.out.println(e);
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>I tried changing my XSL file multiple times to remove extra spaces and to format the output but I am not able to figure out exactly what needs to be done to get an output like this. Can someone please help me on this.</p>