Hi All,
I have an XML file as
Stored in c:\output.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="output.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
</catalog>
And i want to convert it to csv format so i wrote the xsl file as
Stored in c:\output.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates></xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<xsl:apply-templates select="title"></xsl:apply-templates>
<xsl:apply-templates select="artist"></xsl:apply-templates>
<xsl:apply-templates select="country"></xsl:apply-templates>
<xsl:apply-templates select="company"></xsl:apply-templates>
<xsl:apply-templates select="price"></xsl:apply-templates>
<xsl:apply-templates select="year"></xsl:apply-templates>
</xsl:template>
<xsl:template match="title">
<xsl:value-of select="."></xsl:value-of>,
</xsl:template>
<xsl:template match="artist">
<xsl:value-of select="."></xsl:value-of>,
</xsl:template>
<xsl:template match="country">
<xsl:value-of select="."></xsl:value-of>,
</xsl:template>
<xsl:template match="company">
<xsl:value-of select="."></xsl:value-of>,
</xsl:template>
<xsl:template match="price">
<xsl:value-of select="."></xsl:value-of>,
</xsl:template>
<xsl:template match="year">
<xsl:value-of select="."></xsl:value-of>
<br></br>
</xsl:template>
</xsl:stylesheet>
and the java code i am using is
String xsltFile = "c:\\output.xsl";
String xmlsource = "c:\\output.xml";
String locationOfNewContentXml = "c:\\result.csv";
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Source xsltSource = new StreamSource(xsltFile);
File xmlSourceFile = new File(xmlsource);
try
{
Transformer sourcetransormer = transformerFactory.newTransformer(xsltSource);
OutputStream yourFileOutputStream = new FileOutputStream(new File(locationOfNewContentXml));
sourcetransormer.transform(new StreamSource(xmlSourceFile), new StreamResult(new OutputStreamWriter(yourFileOutputStream)));
yourFileOutputStream.close();
}
catch(Exception e)
{
e.printStackTrace();
}
The csv file being produced contains A1 = Empire Burlesque, Bob Dylan, USA, Columbia, 10.90, 1985
and the next row in next A1 and so on.
Can anyone suggest me where am i wrong?