Monday, February 20, 2006

Dom4j SAXParseException performing XSLT with JSTL 1.0 transform tag

Background:
Trying to perform an XSL Transformation of a Dom4J Document in some JSP. Receiving SAXParseException.

Problem Description:
The Following Code:
Document searchResults = ...

<c:import url="/servlet/test.xsl" var="stylesheet" />

<x:transform xml="${
searchResults}" xslt="${stylesheet}">
<x:param name="currentUrlNoQueryString" value="<%=request.getRequestURI()%>"/>
<x:param name="queryStringNoSortType" value="<%=queryStringNoSortType%>"/>
<x:param name="selectedSortType" value='<%=request.getParameter("selectedSortType")%>'/>
</x:transform>

is producing:
javax.xml.transform.TransformerException: org.xml.sax.SAXParseException: Document root element is missing.


Problem Solution:
The solution was that my path to the XSL file (ie. "/servlet/test.xsl") was not being resolved properly.

ie. I changed it explicitly to:
<c:import url="http://servername/servlet/test.xsl" var="stylesheet" />

and it worked. I Guess the error message wasn't intuitive enough for me at first (a lot of other forum posts eg. this one and this one seem to suggest the same error message can also be caused by invalid ie. non UTF characters at the beginning of the XML file).

As a side-note, I was unable to find a JSTL 1.0 API specification anywhere. The JSTL 1.1 API Specification for the x:transform tag deprecates the xml attribute and the 1.0 specification doesn't yet support the doc attribute.

0 Comments:

Post a Comment

<< Home