aspose file tools*
The moose likes XML and Related Technologies and the fly likes Exception -- Content is not allowed in prolog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Exception -- Content is not allowed in prolog" Watch "Exception -- Content is not allowed in prolog" New topic
Author

Exception -- Content is not allowed in prolog

M. Gagnon
Ranch Hand

Joined: Feb 18, 2004
Posts: 68
I'm trying to transform and xml document with an xsl stylesheet using the JSTL x:transform tag in a JSP. My import statement is:

<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>

and the tag is used as follows:

<x:transform xml="ConcertProgram.xml" xslt="ConcertProgram.xsl"/>

When I start Tomcat and go to the JSP, I get the following exception:

javax.servlet.ServletException: [...] org.xml.sax.SAXParseException: Content is not allowed in prolog

This exception makes no sense as my XML file begins thus:

<?xml version="1.0"?>

NO content in prolog. Anyone seen this error before? Anyone found a fix for it?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

Even though you are doing this with JSTL in a JSP, I think the problem lies in the XML or XSLT files, so I'm going to move this over to the XML forum where the XSL-literate hang out.

You are probably going to need to provide the files for perusal.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
M. Gagnon
Ranch Hand

Joined: Feb 18, 2004
Posts: 68
Actually, I'm 99% sure it's a problem with JSTL (or perhaps Xalan) because if I specify the stylesheet in the XML file and view it in Netscape, it's perfectly ok, so the XML and XSL files are fine. I can't do that in the application for two reasons: (1) It doesn't work in IE; (2) I need other JSP content in the page, so it needs to be a JSP, not an XML file.
M. Gagnon
Ranch Hand

Joined: Feb 18, 2004
Posts: 68
The problem really was with my use of JSTL, not the XML (everything was transforming perfectly in Netscape simply by opening the XML file and specifying the XSL style sheet in that file). I'll post the solution in case anyone else has experienced the same problem.

Hardcoding the file names in the tag doesn't work:
<x:transform xml="ConcertProgram.xml" xslt="ConcertProgram.xsl"/>

You have to import the files with the c:import tag:
<c:import url="ConcertProgram.xml" var="xml" />
<c:import url="ConcertProgram.xsl" var="xsl" />
<x_rt:transform xml="${xml}" xslt="${xsl}" />

However, since my hosting service has not upgraded to Servlets 2.4/JSP 2.0, I cannot use the new Expression Language. But when I tried the following, it also did not work:

<c:import url="ConcertProgram.xml" var="xml" />
<c:import url="ConcertProgram.xsl" var="xsl" />
<x_rt:transform xml="<%= xml %>" xslt="<%= xsl %>" />

Even though I can use the scripting syntax here, the variables declared in the c:import tag are not recognized. The following does work:

<c:import url="ConcertProgram.xml" var="xml" scope="application" />
<c:import url="ConcertProgram.xsl" var="xsl" scope="application"/>
<x_rt:transform xml="<%= application.getAttribute("xml") %>" xslt="<%= application.getAttribute("xsl") %>" />

In fact, what's really great is that I can still specify the xml and xsl files in context-param tags in the deployment descriptor file for the application and access them using the following:

<c_rt:import url="<%= application.getInitParameter("concertXML") %>" var="xml" scope="application" />
<c_rt:import url="<%= application.getInitParameter("concertXSL") %>" var="xsl" scope="application" />
<x_rt:transform xml="<%= application.getAttribute("xml") %>" xslt="<%= application.getAttribute("xsl") %>" />

All that through trial and error! Ugh.
Matthias Engelhardt
Greenhorn

Joined: Sep 06, 2005
Posts: 2
I came to this entry by Google; this is perhaps an explanation why my answer is so late.

I got the same error, trying to transform a XML file with a XSLT, using plain javax.xml.transform. After some trial and error I found that this message is only another way to say "Cannot read the XML input file".
This input file is often given as an InputStream, and the many types of InputStream allow the programmer some errors!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception -- Content is not allowed in prolog