You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Steve Krulewitz <sh...@mm.st> on 2004/03/23 05:48:09 UTC
Serving XHTML with Cocoon (was: Empty elements and the XML Serializer)
Thanks to everyone who responded to the "Empty elements and the XML
Serializer" thread. Rather than change the way documents are
serialized, the proper solution is to deliver the content to the browser
in such a way that the browser knows it is getting XHTML.
First of all, you need to specify an XHTML doctype and use the XHTML
namespace for the xhtml elements in your document. You also need to
send the "application/xhtml+xml" content type with your response. But
here is the problem -- Internet Explorer does not like this, and will
attempt to download the page if it receives this content type. It seems
to work for most other browsers, but for Internet Explorer you still
need to send the content as "text/html".
To handle this in Cocoon, I've declared two separate serializers:
<map:serializer
name="xhtml"
mime-type="application/xhtml+xml; charset=utf-8"
src="org.apache.cocoon.serialization.XMLSerializer"
>
<doctype-public>-//W3C//DTD XHTML 1.0 Strict//EN</doctype-public>
<doctype-system>http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd</doctype-system>
<encoding>UTF-8</encoding>
<indent>yes</indent>
</map:serializer>
<map:serializer
name="xhtml-ie"
mime-type="text/html; charset=utf-8"
src="org.apache.cocoon.serialization.XMLSerializer"
>
<doctype-public>-//W3C//DTD XHTML 1.0 Strict//EN</doctype-public>
<doctype-system>http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd</doctype-system>
<encoding>UTF-8</encoding>
<indent>yes</indent>
</map:serializer>
And also a resource that is used in place of <map:serialize/>:
<map:resources>
<map:resource name="serialize">
<map:select type="browser">
<map:when test="explorer">
<map:serialize type="xhtml-ie"/>
</map:when>
<map:otherwise>
<map:serialize type="xhtml"/>
</map:otherwise>
</map:select>
</map:resource>
</map:resources>
So instead of calling <map:serialize/>, i call <map:call
resource="serialize"/>.
The bad part about using a resource is that, as far as I know, a
resource declared in the root sitemap can not be called by subsitemaps.
Is this on par with other people's experience dealing with this problem?
cheers,
-steve
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Serving XHTML with Cocoon - Another solution
Posted by Steve Krulewitz <sh...@mm.st>.
I really wasn't happy with using all that pipeline stuff just because
Internet Explorer does not understand "application/xhtml+xml". In the
non-Cocoon world, it seems like the answer to this problem is adding
rewrite rules to Apache to modify the response based on the user agent.
This is explained clearly in Mark Pilgrim's "The Road to XHTML 2.0:
MIME Types" [1]. If you are not using Apache, another option is to use
a servlet filter. The following code is more of a general content type
replacer, and it works on Tomcat:
package com.skrul.filters;
import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/*
* Much of this code was borred from
*
http://www-106.ibm.com/developerworks/java/library/j-tomcat/?open&l=101,t=grj,p=TomcatTricks
*
*/
public class IEContentTypeFilter implements Filter {
String userAgent = null;
Map replace = null;
public void init(FilterConfig fc) throws ServletException {
userAgent = fc.getInitParameter("useragent");
replace = new HashMap();
Enumeration e = fc.getInitParameterNames();
while(e.hasMoreElements()) {
String key = (String) e.nextElement();
if(key.startsWith("search_")) {
String a[] = key.split("_");
replace.put(fc.getInitParameter(key),
fc.getInitParameter("replace_" + a[1]));
}
}
replace = Collections.unmodifiableMap(replace);
}
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
String browserDet = ((HttpServletRequest)
request).getHeader("User-Agent").toLowerCase();
if (browserDet.indexOf(userAgent) != -1) {
ReplaceContentTypeWrapper wrapped = new
ReplaceContentTypeWrapper(response, replace);
chain.doFilter(request, wrapped);
}
else {
chain.doFilter(request, response);
}
}
public void destroy() {
}
class ReplaceContentTypeWrapper extends HttpServletResponseWrapper {
String search;
Map replace;
public ReplaceContentTypeWrapper(ServletResponse inResp, Map
replace) throws java.io.IOException {
super((HttpServletResponse) inResp);
this.replace = replace;
}
public void setContentType(String contentType) {
if(replace.containsKey(contentType)) {
super.setContentType((String) replace.get(contentType));
}
else {
super.setContentType(contentType);
}
}
}
}
You need to configure this in your web.xml, and the following lines must
come under the web-app element but before the servlet element:
<filter>
<filter-name>IEContentTypeFilter</filter-name>
<filter-class>com.skrul.filters.IEContentTypeFilter</filter-class>
<init-param>
<param-name>useragent</param-name>
<param-value>msie</param-value>
</init-param>
<init-param>
<param-name>search_1</param-name>
<param-value>application/xhtml+xml; charset=utf-8</param-value>
</init-param>
<init-param>
<param-name>replace_1</param-name>
<param-value>text/html; charset=utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>IEContentTypeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
cheers,
-steve
[1] http://www.xml.com/pub/a/2003/03/19/dive-into-xml.html
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: Serving XHTML with Cocoon (was: Empty elements and the XML
Serializer)
Posted by Jean Pierre LeJacq <jp...@quoininc.com>.
On Tue, 23 Mar 2004, Steve Krulewitz wrote:
> > Hmmm, when I try setting the mime type to application/xhtml+xml,
> > mozilla and firefox both render as XML instead of XHTML. I see
> > the same HTTP headers you do. Is there a trick to tell Mozilla
> > to render as XHTML?
>
> Are you sure the xhtml elements in your document are in the xhtml
> namespace?
Yes! This did the trick. I hadn't noticed that one of the lenya
transformers was stripping this namespace out. Thank you.
--
JP
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: Serving XHTML with Cocoon (was: Empty elements and the XML Serializer)
Posted by Steve Krulewitz <sh...@mm.st>.
> Hmmm, when I try setting the mime type to application/xhtml+xml,
> mozilla and firefox both render as XML instead of XHTML. I see the
> same HTTP headers you do. Is there a trick to tell Mozilla to
> render as XHTML?
Are you sure the xhtml elements in your document are in the xhtml namespace?
cheers,
-steve
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: Serving XHTML with Cocoon (was: Empty elements and the XML
Serializer)
Posted by Jean Pierre LeJacq <jp...@quoininc.com>.
On Tue, 23 Mar 2004, Steve Krulewitz wrote:
> Using this technique, the textarea does render properly on Firefox 0.8
> *only* when the pages are served with the "application/xhtml+xml"
> content type. As soon as I changed the content type to "text/html", I
> got the incorrectly rendered textarea. This is the test document I'm using:
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml">
> <head>
> <title>title</title>
> </head>
> <body>
> <form action="">
> <textarea name="ta" rows="10" cols="10" />
> </form>
> </body>
> </html>
>
> And the HTTP headers:
>
> HTTP/1.x 200 OK
> X-Cocoon-Version: 2.1.4
> Last-Modified: Tue, 23 Mar 2004 14:58:55 GMT
> Content-Type: application/xhtml+xml; charset=utf-8
> Content-Length: 338
> Date: Tue, 23 Mar 2004 15:06:47 GMT
Hmmm, when I try setting the mime type to application/xhtml+xml,
mozilla and firefox both render as XML instead of XHTML. I see the
same HTTP headers you do. Is there a trick to tell Mozilla to
render as XHTML?
--
JP
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: Serving XHTML with Cocoon (was: Empty elements and the XML Serializer)
Posted by Steve Krulewitz <sh...@mm.st>.
> I see two problem.
>
> * This will not solve the problem in Mozilla. Even with
> properly declared doctype and a valid document, Mozilla
> will render <textarea/> incorrectly. Its a bug in Mozilla. I
> didn't check to see if its in Mozilla's bug database.
Using this technique, the textarea does render properly on Firefox 0.8
*only* when the pages are served with the "application/xhtml+xml"
content type. As soon as I changed the content type to "text/html", I
got the incorrectly rendered textarea. This is the test document I'm using:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>title</title>
</head>
<body>
<form action="">
<textarea name="ta" rows="10" cols="10" />
</form>
</body>
</html>
And the HTTP headers:
HTTP/1.x 200 OK
X-Cocoon-Version: 2.1.4
Last-Modified: Tue, 23 Mar 2004 14:58:55 GMT
Content-Type: application/xhtml+xml; charset=utf-8
Content-Length: 338
Date: Tue, 23 Mar 2004 15:06:47 GMT
Server: Apache Coyote/1.0
> * I've run into a problem with the cocoon serializer not
> respecting content type UTF-8. I've only been able to get
> ISO Latin to work.
I noticed this as well. To help deal with this, I added the charset to
the mime type on the serializer:
mime-type="application/xhtml+xml; charset=utf-8"
cheers,
-steve
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: Serving XHTML with Cocoon (was: Empty elements and the XML Serializer)
Posted by beyaNet Consultancy <be...@ntlworld.com>.
Hi,
I have changed my site map to include the following xhtml serializer:
<map:serializer name="xhtml"
src="org.apache.cocoon.serialization.XMLSerializer"
mime-type="text/html" logger="sitemap.serializer.xhtml" pool-grow="2"
pool-max="64" pool-min="2">
<doctype-public>-//W3C//DTD XHTML 1.0 Strict//EN</doctype-public>
<doctype-system>http://www.w3.org/TR/xhtml1/DTD/xhtml1-
traditional.dtd</doctype-system>
<encoding>UTF-8</encoding>
<indent>yes</indent>
</map:serializer>
Now when i try and load the site in Safari 1.2.1 nothing happens. No
error is displayed, and if you view source the page the code is there
to be seen, but the page is not being displayed. What is happening
here?? The site displays fine in both Netscape 7.1 and Mozilla 1.7a.
I am running on OS X 10.3.3, Apache tomcat 5, cocoon 2.1.4
Peter
On 23 Mar 2004, at 08:59, Jean Pierre LeJacq wrote:
> On Tue, 23 Mar 2004, beyaNet Consultancy wrote:
>
>> would i be right in thinking that the following sitemap snippet does
>> not actually turn my document into an xhtml document!?:
>>
>> <map:serializer logger="sitemap.serializer.html"
>> mime-type="text/html" name="html" pool-grow="4" pool-max="32"
>> pool-min="4" src="org.apache.cocoon.serialization.HTMLSerializer">
>> <doctype-public>-//W3C//DTD XHTML 1.0
>> Transitional/EN</doctype-public>
>>
>> <doctype-system>http://www.w3.org/TR/xhtml1/DTD/xhtml1-
>> transitional.dtd</doctype-system>
>> </map:serializer>
>
> Assuming you have the paths correct, that should do it. Its simple
> enough to validate for yourself. Just submit it to the www.w3.org
> validator.
>
> The only change you might want to consider is adding:
>
> <media-type>application/xhtml+xml</media-type>
>
> Unfortunately, IE doesn't recognize this so its not particularly
> useful. See:
>
> http://www.w3.org/TR/2002/NOTE-xhtml-media-types-20020801/
>
> --
> JP
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
Re: Serving XHTML with Cocoon (was: Empty elements and the XML
Serializer)
Posted by Jean Pierre LeJacq <jp...@quoininc.com>.
On Tue, 23 Mar 2004, beyaNet Consultancy wrote:
> would i be right in thinking that the following sitemap snippet does
> not actually turn my document into an xhtml document!?:
>
> <map:serializer logger="sitemap.serializer.html"
> mime-type="text/html" name="html" pool-grow="4" pool-max="32"
> pool-min="4" src="org.apache.cocoon.serialization.HTMLSerializer">
> <doctype-public>-//W3C//DTD XHTML 1.0
> Transitional/EN</doctype-public>
>
> <doctype-system>http://www.w3.org/TR/xhtml1/DTD/xhtml1-
> transitional.dtd</doctype-system>
> </map:serializer>
Assuming you have the paths correct, that should do it. Its simple
enough to validate for yourself. Just submit it to the www.w3.org
validator.
The only change you might want to consider is adding:
<media-type>application/xhtml+xml</media-type>
Unfortunately, IE doesn't recognize this so its not particularly
useful. See:
http://www.w3.org/TR/2002/NOTE-xhtml-media-types-20020801/
--
JP
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: Serving XHTML with Cocoon (was: Empty elements and the XML Serializer)
Posted by beyaNet Consultancy <be...@ntlworld.com>.
Hi,
would i be right in thinking that the following sitemap snippet does
not actually turn my document into an xhtml document!?:
<map:serializer logger="sitemap.serializer.html"
mime-type="text/html" name="html" pool-grow="4" pool-max="32"
pool-min="4" src="org.apache.cocoon.serialization.HTMLSerializer">
<doctype-public>-//W3C//DTD XHTML 1.0
Transitional/EN</doctype-public>
<doctype-system>http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd</doctype-system>
</map:serializer>
Peter
On 23 Mar 2004, at 05:47, Jean Pierre LeJacq wrote:
> On Mon, 22 Mar 2004, Steve Krulewitz wrote:
>
>> First of all, you need to specify an XHTML doctype and use the XHTML
>> namespace for the xhtml elements in your document. You also need to
>> send the "application/xhtml+xml" content type with your response. But
>> here is the problem -- Internet Explorer does not like this, and will
>>
>> ...
>>
>> <map:serializer
>> name="xhtml-ie"
>> mime-type="text/html; charset=utf-8"
>> src="org.apache.cocoon.serialization.XMLSerializer"
>>>
>> <doctype-public>-//W3C//DTD XHTML 1.0 Strict//EN</doctype-public>
>>
>> <doctype-system>http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd</
>> doctype-system>
>> <encoding>UTF-8</encoding>
>> <indent>yes</indent>
>> </map:serializer>
>>
>> ...
>>
>> Is this on par with other people's experience dealing with this
>> problem?
>
> I see two problem.
>
> * This will not solve the problem in Mozilla. Even with
> properly declared doctype and a valid document, Mozilla
> will render <textarea/> incorrectly. Its a bug in Mozilla. I
> didn't check to see if its in Mozilla's bug database.
>
> * I've run into a problem with the cocoon serializer not
> respecting content type UTF-8. I've only been able to get
> ISO Latin to work. See:
>
> http://nagoya.apache.org/bugzilla/show_bug.cgi?id=26997
>
> --
> JP
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
Re: Serving XHTML with Cocoon (was: Empty elements and the XML
Serializer)
Posted by Jean Pierre LeJacq <jp...@quoininc.com>.
On Mon, 22 Mar 2004, Steve Krulewitz wrote:
> First of all, you need to specify an XHTML doctype and use the XHTML
> namespace for the xhtml elements in your document. You also need to
> send the "application/xhtml+xml" content type with your response. But
> here is the problem -- Internet Explorer does not like this, and will
>
> ...
>
> <map:serializer
> name="xhtml-ie"
> mime-type="text/html; charset=utf-8"
> src="org.apache.cocoon.serialization.XMLSerializer"
> >
> <doctype-public>-//W3C//DTD XHTML 1.0 Strict//EN</doctype-public>
>
> <doctype-system>http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd</doctype-system>
> <encoding>UTF-8</encoding>
> <indent>yes</indent>
> </map:serializer>
>
> ...
>
> Is this on par with other people's experience dealing with this problem?
I see two problem.
* This will not solve the problem in Mozilla. Even with
properly declared doctype and a valid document, Mozilla
will render <textarea/> incorrectly. Its a bug in Mozilla. I
didn't check to see if its in Mozilla's bug database.
* I've run into a problem with the cocoon serializer not
respecting content type UTF-8. I've only been able to get
ISO Latin to work. See:
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=26997
--
JP
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org