Remove the line (reference to xsl file)
<?xml:stylesheet type="text/xsl" href="">
from foo.xml and try to do transformation. Hope this works.
Thanks,
Prakash
-----Original Message-----
From: Priya Mahajan [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 26, 2003 9:28 PM
To: [EMAIL PROTECTED]
Subject: transfomer.transfor error when using a DOMSource object
Hi all,
I am using Xalan 2.5.1 to transform some xml document in a servlet and send the
output html to the browser. I am using a foo.xml and foo.xsl to do this.
When I do something like
<code>
Source xmlSource = new StreamSource(new URL(LOCNXML + "foo.xml").openStream());
</code>
this works perfectly. But I need to actually create the xmlSource from a DOM
object as I have to do some processing on the XML before applying the
stylesheet to it. If I then do
<code>
Document doc = ......;
Source xmlSource = new DOMSource(doc);
</code>
I get the following error
--------------------------------------------------------------------------
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then
click the Refresh button, or try again later.
Invalid at the top level of the document. Error processing resource "..."
Line 1, Position 39
<?xml version="1.0" encoding="UTF-8"?>
---------------------------------------------------------------------------
I'm also copying my servlet and xml, xsl files for testing.
----------------------------------------------------------------------
<servlet>
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.net.URL;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class XMLServlet extends HttpServlet {
public final static String LOCNXML = "file:///c:/xml/";
public final static String LOCNXSL = "file:///c:/xsl/";
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException, java.net.MalformedURLException
{
// The servlet returns HTML.
response.setContentType("text/html; charset=UTF-8");
// Output goes in the response stream.
PrintWriter out = response.getWriter();
try
{
DocumentBuilderFactory dbfact = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfact.newDocumentBuilder();
Document doc = docBuilder.parse(LOCNXML + "foo.xml");
TransformerFactory tFactory = TransformerFactory.newInstance();
// Get the XML input document and the stylesheet.
Source xmlSource = new DOMSource(doc);
Source xslSource = new StreamSource(new URL(LOCNXSL
+ "foo.xsl").openStream());
// Generate the transformer.
Transformer transformer = tFactory.newTransformer(xslSource);
transformer.transform(xmlSource, new StreamResult(out));
}
catch (Exception e)
{
out.write(e.getMessage());
e.printStackTrace(out);
}
out.close();
}
}
</servlet>
--------------------------------------------------------------
<foo.xml>
<?xml version="1.0" encoding="UTF-8"?>
<?xml:stylesheet type="text/xsl" href="">
<Library_Contents xmlns:cseo="http://www.cseo.net/xml/rules/">
<data>blah</data>
<Library>
<book>Professional XML Databases</book>
</Library>
<data>xyz</data>
</Library_Contents>
</foo.xml>
------------------------------------------------------------------
<foo.xsl>
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head><title>Customer</title></head>
<body>
<xsl:value-of select="."/>
</body>
</html>
</xsl:template></xsl:stylesheet>
</foo.xsl>
Does anyone know what I'm doing wrong here ? I'd really appreciate any help I
can get,
Regards,
Priya
