Monday, June 20, 2005

Using JSP comments in XML generating pages

I now use JSP extensively to make pages that create XML. XML (done properly) is very fussy, everything has to be well formed. The JSP below creates an XML document with a linespace at the top.


<%@ page contentType="text/xml"%>
<?xml version="1.0" encoding="UTF-8"?>
<xml />

IE blindly accepts it but Firefox baulks at it and reports the error:

XML Parsing Error: xml processing instruction not at start of external entity

Now Firefox is right to do this, this is exactly what an application consuming this XML page would complain about. So how do I fix it? Well, I could put all the JSP on a single line but this makes it difficult for me to read, I am only human. JSP comments makes it easier to produce good XML pages and maintain some form of human readability.


<%@ page contentType="text/xml"%><%--
--%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %><%--
--%>
<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %><%--
--%>
<?xml version="1.0" encoding="UTF-8"?>
<xml />

1 comments:

Mark McLaren said...

thanks for help me! The trick of works for me... ;)
Note: Comment imported. Original by Nuno at 2008-08-13 02:40