I am trying to transform one form of XML to another using xslt and sax parser. Here are my sample inputxml and xsl snippets.
Input XML:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<AXL>
<ROW>
<firsname>John</firstname>
<lastName>Smith</Smith>
</ROW>
<ROW>
<firstname>George</firstname>
<lastName>Tack</Smith>
</ROW>
</AXL>
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="text()">
<xsl:value-of select='normalize-space()'/>
</xsl:template>
<xsl:template match="/">
<!--bom:AML-->
<xsl:for-each select="//row">
<Contact>
<xsl:attribute name="firstName"><xsl:value-of select="firstName"/></xsl:attribute>
<xsl:attribute name="lastName"><xsl:value-of select="lastName"/></xsl:attribute>
</Contact>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I could generate the output.xml using online tools. But with java code, i am just getting an empty output file. I know I messed it up somewhere but don't know where??
I appreciate your help!
Ok, here is my java code. And it is <firstName>
. Sorry, typo error.
System.setProperty("javax.xml.transform.TransformerFactory",
"net.sf.saxon.TransformerFactoryImpl");
TransformerFactory factory = TransformerFactory.newInstance();
Templates template = factory.newTemplates(
new StreamSource(new FileInputStream("C:\\XML_Transformation.xsl")));
Transformer transformer = template.newTransformer();
Source source = new StreamSource(new FileInputStream("C:\\Sample.xml"));
Result result = new StreamResult(new FileOutputStream("C:\\Final.xml"));
transformer.transform(source, result);