Sirviendo documentos XHTML válidos para distintos navegadores

4 views
Skip to first unread message

webn...@gmail.com

unread,
Jul 16, 2006, 10:44:52 AM7/16/06
to WebNewbie
Los documentos XHTML cada vez está teniendo más auge en muchos sitios
Web, entre muchos de esos documentos están bien formados pero no
válidos. Antes de enviar un documento XHTML, tenemos que saber si el
navegador en que vamos a mostrar el DTD (Definición del tipo de
documento) lo hacepta como tal, en caso contrario, tengamos otra
opción y enviarselos, como por ejemplo: text/html o aplication/xml.


Pues existen muchas formas de preguntar, una de esas es en la que
expongo en el siguiente artículo:

http://webnewbie.org/modules.php?name=News&file=article&sid=50


Algo simple y fundamental, pero para algo más exacto sería como lo
siguiente:


Código:
######################################################################
if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {


if(preg_match("/application\/xhtml\+xml;q=0(\.[1-9]+)/i",$_SERVER["HTTP_ACCEPT"],$matches))
{

$xhtml_q = $matches[1];


if(preg_match("/text\/html;q=0(\.[1-9]+)/i",$_SERVER["HTTP_ACCEPT"],$matches))
{

$txt_html_q = $matches[1];

if($xhtml_q >= $txt_html_q) {

define('BROWSER_PREFER_XHTML', TRUE);

define('BROWSER_PREFER_XML', FALSE);

}
elseif(preg_match("/application\/xml;q=0(\.[1-9]+)/i",$_SERVER["HTTP_ACCEPT"],$matches)){


$xml_q = $matches[1];

if($xml_q >= $txt_html_q) {

define('BROWSER_PREFER_XML', TRUE);

define('BROWSER_PREFER_XHTML', FALSE);

}else {

define('BROWSER_PREFER_XML', FALSE);

define('BROWSER_PREFER_XHTML', FALSE);

}


}elseif(stristr($_SERVER["HTTP_ACCEPT"],"application/xml")) {

define('BROWSER_PREFER_XML', TRUE);

define('BROWSER_PREFER_XHTML', FALSE);

} else {

define('BROWSER_PREFER_XML', FALSE);

define('BROWSER_PREFER_XHTML', FALSE);

}

}

} else {

define('BROWSER_PREFER_XHTML', TRUE);

define('BROWSER_PREFER_XML', FALSE);

}

} else {

define('BROWSER_PREFER_XHTML', FALSE);

define('BROWSER_PREFER_XML', FALSE);

}

if(BROWSER_PREFER_XHTML) $mime = "application/xhtml+xml";

elseif(BROWSER_PREFER_XML) $mime = "application/xml";

else {

//For aplication/xhtml

$brws_accept_xhtml = array('NetFront 3.0', 'Amaya 8.5', 'Amaya
8.5', 'Amaya 8.5', 'Amaya 8.4', 'Amaya 8.3', 'Amaya 8.2', 'Amaya 8.1b',
'Amaya 8.0', 'iCab 3.0b', 'iCab 2.9.8', 'iCab 2.9.7', 'iCab 2.9.6',
'iCab 2.9.5', 'iCab 2.9.1', 'iCab 2.9', 'iCab 2.8.2', 'Netscape 8.0.2',
'Opera 7.03', 'Opera 7.02', 'Opera 7.01', 'Opera 7.0', 'Opera 6.06',
'Opera 6.05', 'Opera 6.04', 'Opera 6.03', 'Opera 6.02', 'Opera 6.01',
'Opera 6.0', 'xfy BE1.0b-r3', 'xfy BE1.0b-r2', 'xfy TP2');

//For aplication/xml

$brws_accept_xml = array('MSIE 8.0', 'MSIE 7.0', 'MSIE 6.0', 'MSIE
5.5', 'MSIE 5.01', 'MSIE 5.0', 'Opera 5.12', 'Opera 5.0', 'xfy
BE1.0b');

if(in_array($XnUser_Agent->Agent_browser(), $brws_accept_xhtml)) {

$mime = "application/xhtml+xml";

$in_out = 1;

}elseif(in_array($XnUser_Agent->Agent_browser(), $brws_accept_xml))
$mime = "application/xml";

}

if($mime == "application/xhtml+xml") {

$docstart = "$XML$DTD";

}elseif($mime == "application/xml"){

$docstart = "$xml$Dtd";

}else {

$docstart = "$quirksmode$dtd";

}


header("Content-Type: $mime; charset=$charset");

header("Vary: Accept");

print $docstart;

######################################################################

Como el tema aquí expuesto, es sobre servir documentos XHTML válidos
para navegadores, solo hablaremos de ello.

Cada uno de los documentos deben de ser antecedidos por una
declaración de xml:

Código:
#####################################################################
<?xml version="1.0" encoding="UTF-8"?>
####################################################################

Y se pueden declarar hojas de estilo después la declaración de xml,
antes de la declaración de la DTD:

Código:
#####################################################################
<?xml-stylesheet type="text/css" href="xhtml.css"?>
####################################################################


Para un documento XHTML 1.0, se tiene estas tres DTD:

1. Para un documento que no debe de tener elementos no aceptados.

Código:
#####################################################################
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
#####################################################################

2. Para un documento que acepta elementos no aceptados.

Código:
#####################################################################
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
####################################################################


3. Para documentos que usan Frames.

Código:
######################################################################
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
#####################################################################


Para un documento que se usa XHTML 1.1 se usa la única DTD:

Código:
#####################################################################
<!DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
###################################################################


Y por último tenemos a XHTML 2.0.

Código:
######################################################################
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 2.0//EN"
"http://www.w3.org/MarkUp/DTD/xhtml2.dtd">
#####################################################################

Éste último, aún no es oficial. Mientras tanto, debemos de valer
usando hojas de estilo como XSL, XSLT y/o CSS.

Por último se debe de asignar un espacio de nombres para cada DTD:

Para XHTML 1.0

Código:
#####################################################################
<html lang="es" xml:lang="es" xmlns="http://www.w3.org/1999/xhtml">
####################################################################


Para XHTML 1.1

Código:
######################################################################
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" >
#####################################################################


Para XHMTL 2.0

Código:
######################################################################
<html xmlns="http://www.w3.org/2002/06/xhtml2/" xml:lang="es"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2002/06/xhtml2/
http://www.w3.org/MarkUp/SCHEMA/xhtml2.xsd"
>
######################################################################

Como ejemplo, exponemos un documento XHTML 2.0:

Código:
#####################################################################
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css"
href="http://www.w3.org/MarkUp/style/xhtml2.css"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 2.0//EN"
"http://www.w3.org/MarkUp/DTD/xhtml2.dtd">
<html xmlns="http://www.w3.org/2002/06/xhtml2/" xml:lang="en"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2002/06/xhtml2/
http://www.w3.org/MarkUp/SCHEMA/xhtml2.xsd"
>
<head>
<title>Biblioteca Virtual</title>
</head>
<body>
<p>Mover a <a href="http://ejemplo.org/">ejemplo.org</a>.</p>
</body>
</html>
######################################################################

webn...@gmail.com

unread,
Aug 13, 2006, 7:22:26 PM8/13/06
to WebNewbie
Basándose en una simple condición en PHP se podría hacer con el
siguiente código:

######################################################################
<?php
if ((isset($_SERVER["HTTP_ACCEPT"]) and
stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml")) or
stristr($_SERVER["HTTP_USER_AGENT"], "W3C_Validator") )
{
header("Content-type: application/xhtml+xml");
print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
}
else
{
header("Content-type: text/html; charset=utf-8");
}
?>
######################################################################

Aunque claro, esto no es completo como el de arriba, pero lo hago para
comparar con otros lenguajes.


En Ruby:
######################################################################
<%
if @request.env["HTTP_ACCEPT"].index('application/xhtml+xml')
@headers["Content-Type"] = "application/xhtml+xml; charset=utf-8"
else
@headers["Content-Type"] = "text/html; charset=utf-8"
end
%>
######################################################################


En Python (CGI script)
######################################################################
import os
if os.environ.get('HTTP_ACCEPT', '').find('application/xhtml+xml') >
-1:
print 'Content-type: application/xhtml+xml'
else: print 'Content-type: text/html'
######################################################################


Y finalmente, para servir los archivos estáticos como archivos html,
se puede hacer con apache en el archivo .htaccess

######################################################################
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml\s*;\s*q=0
RewriteCond %{REQUEST_URI} \.html$
RewriteCond %{THE_REQUEST} HTTP/1\.1
RewriteRule .* - [T=application/xhtml+xml]
######################################################################

Reply all
Reply to author
Forward
0 new messages