Ronayne
asked on
xml/xsl code not working
Hi, below is a jsp file thats supposed to take an xml file and write it to an xsl file but I dont think its working, am I missing anything?, thanks.
<%@ page import = "java.io.*"%>
<%@ page import = "org.w3c.dom.*"%>
<%@ page import = "org.xml.sax.*"%>
<%@ page import = "javax.xml.parsers.*"%>
<%@ page import = "javax.xml.transform.*"%>
<%@ page import = "javax.xml.transform.dom.* "%>
<%@ page import = "javax.xml.transform.strea m.*"%>
<%
// This method applies the xslFilename to inFilename and writes
// the output to outFilename.
String inFilename = "C:\\Documents and Settings\\Adrian Ron\\Start Menu\\Programs\\Apache Tomcat 4.1\\xmlfile.xml";
String xslFilename = "xsl";
// Writer out;
try {
// Create transformer factory
TransformerFactory factory = TransformerFactory.newInst ance();
// Use the factory to create a template containing the xsl file
Templates template = factory.newTemplates(new StreamSource(
new FileInputStream(xslFilenam e)));
// Use the template to create a transformer
Transformer xformer = template.newTransformer();
// Prepare the input and output files
Source source = new StreamSource(new FileInputStream(inFilename ));
Result result = new StreamResult(out);
// Apply the xsl file to the source file and write the result to the output file
xformer.transform(source, result);
} catch (FileNotFoundException e) {
} catch (TransformerConfigurationE xception e) {
// An error occurred in the XSL file
} catch (TransformerException e) {
// An error occurred while applying the XSL file
// Get location of error in input file
SourceLocator locator = e.getLocator();
int col = locator.getColumnNumber();
int line = locator.getLineNumber();
String publicId = locator.getPublicId();
String systemId = locator.getSystemId();
}
%>
<%@ page import = "java.io.*"%>
<%@ page import = "org.w3c.dom.*"%>
<%@ page import = "org.xml.sax.*"%>
<%@ page import = "javax.xml.parsers.*"%>
<%@ page import = "javax.xml.transform.*"%>
<%@ page import = "javax.xml.transform.dom.*
<%@ page import = "javax.xml.transform.strea
<%
// This method applies the xslFilename to inFilename and writes
// the output to outFilename.
String inFilename = "C:\\Documents and Settings\\Adrian Ron\\Start Menu\\Programs\\Apache Tomcat 4.1\\xmlfile.xml";
String xslFilename = "xsl";
// Writer out;
try {
// Create transformer factory
TransformerFactory factory = TransformerFactory.newInst
// Use the factory to create a template containing the xsl file
Templates template = factory.newTemplates(new StreamSource(
new FileInputStream(xslFilenam
// Use the template to create a transformer
Transformer xformer = template.newTransformer();
// Prepare the input and output files
Source source = new StreamSource(new FileInputStream(inFilename
Result result = new StreamResult(out);
// Apply the xsl file to the source file and write the result to the output file
xformer.transform(source, result);
} catch (FileNotFoundException e) {
} catch (TransformerConfigurationE
// An error occurred in the XSL file
} catch (TransformerException e) {
// An error occurred while applying the XSL file
// Get location of error in input file
SourceLocator locator = e.getLocator();
int col = locator.getColumnNumber();
int line = locator.getLineNumber();
String publicId = locator.getPublicId();
String systemId = locator.getSystemId();
}
%>
what gets displayed?
i'd suggest not ignoring any exceptions so you know if something has gone wrong.
e.printStackTrace();
out.println("Error: "+e);
e.printStackTrace();
out.println("Error: "+e);
ASKER
nothing get displayed, just a blank page
ASKER
what I want to do is open the xml document with a stylesheel and display it as an html document.
check those exceptions, i'm guessing one is getting thrown
if so, the exception should give u an indication of what the problem is.
perhaps it's not finding the xsl file.
if so, the exception should give u an indication of what the problem is.
perhaps it's not finding the xsl file.
ASKER
Well I did'nt create an xsl file, could you guide me as to what I need to do so I can open the xml document with a stylesheel and display it as an html document?
ASKER
Im trying to work off thsi tutorial which I think is sufficient for what I want to do.
http://www.xmlscript.org/docs/Tutorial.2.2.html
What do you think?
http://www.xmlscript.org/docs/Tutorial.2.2.html
What do you think?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for that, im working off this: http://www.w3schools.com/xsl/xsl_transformation.asp. The server will be accessing the xml file on the clients computer and transforming it to html on the clients computer. Here is what I came up with. Can I just put the xml style sheet in a tomcat directory, is this line ok: <?xml-stylesheet type="text/xsl" href="localhost:8080\cdcat alog.xsl"? >. Ill increase the points.
//the xml document
<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml-stylesheet type="text/xsl" href="localhost:8080\cdcat alog.xsl"? >
<Table>
<Product>
<Name>xml</Name>
<Type>isa </Type>
<Amount>file</Amount>
</Product>
</Table>
////the style sheet
<?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>
<h2>My XML File</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Name</th>
<th align="left">Type</th>
<th align="left">Amt</th>
</tr>
<xsl:for-each select="Table/Product">
<tr>
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="Title"/></td>
<td><xsl:value-of select="Amount"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
//the xml document
<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml-stylesheet type="text/xsl" href="localhost:8080\cdcat
<Table>
<Product>
<Name>xml</Name>
<Type>isa </Type>
<Amount>file</Amount>
</Product>
</Table>
////the style sheet
<?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>
<h2>My XML File</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Name</th>
<th align="left">Type</th>
<th align="left">Amt</th>
</tr>
<xsl:for-each select="Table/Product">
<tr>
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="Title"/></td>
<td><xsl:value-of select="Amount"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
ASKER
its ok, that tutorial you posted really helped me, thanks a million