Monday, February 20, 2012

Creating xml based on html using xslt in java


Please help me i am doing project of file conversion that is converting xml to html using xsl in java and also i need to convert html to xml using the xsl. my first process is got over.but i structed in second part of conversion.. Is there is any possibility to do that conversion.I will tell you the exact flow of the first process...



This is my sample xml file: tabl.xml:




<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xml" href="testxsl.xsl"?>
<mainpara>
<epigraph>
<para>Though successful research demands a deep
<emphasis role="italic">trained</emphasis>
<emphasis role="italic">taught</emphasis> to regard.
</para>
<para>Kuhn (1976, p. 66)</para>
</epigraph>
<blockquote role="extract">
<para>Though successful research demands a deep commitment to the status quo.
<emphasis role="italic">trained</emphasis>
<emphasis role="italic">taught</emphasis>
</para>
</blockquote>
</mainpara>



This is my sample xsl file:



testme.xsl




<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="mainpara">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="epigraph">
<div>
<xsl:apply-templates/>
</div>
</xsl:template>

<xsl:template match="para">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>

<xsl:template match="blockquote">
<b>
<xsl:apply-templates/>
</b>
</xsl:template>
</xsl:stylesheet>



This is my sample java file:



Main.java




import java.io.File;
import java.io.InputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {


public static void simpleTransform(String sourcePath, String xsltPath,
String resultDir) {

TransformerFactory tFactory = TransformerFactory.newInstance();
try {
Transformer transformer =
tFactory.newTransformer(new StreamSource(new File(xsltPath)));

transformer.transform(new StreamSource(new File(sourcePath)),
new StreamResult(new File(resultDir)));

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {


System.setProperty("javax.xml.transform.TransformerFactory",
"net.sf.saxon.TransformerFactoryImpl");

simpleTransform("E:/bh/tabl.xml","E:/bh/testme.xsl", "E:/bh/me.html");
//simpleTransform("E:/bh/me.html","E:/bh/11111.xsl","E:/bh/tab.xml" ); //This is i need

}
}



This is my generated html file:




<html>
<body>
<div>

<p>Though successful research demands a deep commitment to the status quo, innovation
</p>

<p>Kuhn (1976, p. 66)</p>

</div>
<b>

<p>Though successful research demands a deep commitment to the status quo
</p>
</b>

</body>
</html>



Now i will edit this html file after edited this html file i need to convert this again to the xml file or xhtml file that is i need the reverse process. I think it is possible only by modifying the xsl. Is it possible to create the xsl based on that html.If i created that xsl file then i pass that xsl file in the above java program( i commented that line in the main java program).So i have to create that xsl file.



Please help me.. I need your help..



Thanks in advance

No comments:

Post a Comment