I just started using XSLT
for transforming an XML
to output as test
file.
Here is my XML input file:
<?xml version="1.0" ?>
<transcript>
<student id="STU12345" name="name 1" status="active">
<home_address>35 Wall Street, Wonderland, NJ</home_address>
<interests>
<interest>interest 1</interest>
<interest>interest 2</interest>
<interest>interest 3</interest>
</interests>
</student>
<term>
<heading name="Winter 1999" />
<course>
<course-name>course 1</course-name>
<grade>A-</grade>
<credits>4</credits>
</course>
<course>
<course-name>course 2</course-name>
<grade>B+</grade>
<credits>3</credits>
</course>
</term>
<summary>summary</summary>
<comments>
comments
</comments>
</transcript>
My XML file to transform this xml is:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no" />
<xsl:template match="transcript">
Student Transcript
<xsl:apply-templates select="student" />
Course Name | Grade | Credits
<xsl:apply-templates select="term" />
</xsl:template>
<xsl:template match="student">
Name: <xsl:value-of select="@name" />
ID: <xsl:value-of select="@id" />
</xsl:template>
<xsl:template match="term"><xsl:apply-templates /></xsl:template>
<xsl:template match="course"><xsl:value-of select="course-name"/> |<xsl:value-of select="grade" /> | <xsl:value-of select="credits" />
</xsl:template>
</xsl:stylesheet>
The output that I am getting now is:
Student Transcript
Name: name 1
ID: STU12345
Course Name | Grade | Credits
course 1 |A- | 4
course 2 |B+ | 3
But I am trying to get the output as:
Student Transcript
Name: name 1
ID: STU12345
Course Name | Grade | Credits
course 1 |A- | 4
course 2 |B+ | 3
Finally my Java program that helps me to do the transformation:
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();
}
}
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.