Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XHTML validation, custom namespaces and attributes

I have a website that a client requires to be XHTML valid. They check it using the W3C validator.

I want to add my own namespace and attributes so I can do things like this:

<div my:userid="123"> ... </div>

From what I understand, defining custom namespaces is perfectly valid XHTML, as long as I do this:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:my="http://www.example.com/my">

However, my XHTML fails validation. The problem appears to be that the validator does not actually go out and check my custom DTD document for my custom namespace, it only checks the XHTML against known DTD's. Anyone able to shed any light on how I can solve this problem?

like image 366
Alex York Avatar asked Feb 24 '09 10:02

Alex York


3 Answers

For XHTML 1.0 you are restricted to XHTML 1.0 elements and attributes:

Normative Definition of XHTML 1.0

This version of XHTML provides a definition of strictly conforming XHTML 1.0 documents, which are restricted to elements and attributes from the XML and XHTML 1.0 namespaces.

My understanding is that XHTML 2.0 aims at providing a framework for doing what you want.

like image 198
kmkaplan Avatar answered Nov 10 '22 11:11

kmkaplan


From what I understand, defining custom namespaces is perfectly valid XHTML

Nope. Custom namespaces are perfectly well-formed in XML, but ‘valid’ has the specific meaning that every element and attribute used is declared in the document's schema. That schema can be a DTD, an XML Schema, or something else, but you've got to declare it.

So you can declare your own schema to add custom attributes to the language, and indeed XHTML Modularization makes this very easy. You'd have to add the reference to the DTD as a <!DOCTYPE> in the prolog; just setting namespace URIs doesn't give an XML processor any hook to find the schema in itself.

But then what you've written is “valid my-language-which-is-a-bit-like-XHTML”, and not “valid XHTML”. Some of these ‘my-languages’ are well-known, like ‘XHTML+MathML+SVG’, but it's still not XHTML as such and if your client is dead set on “valid XHTML” you can't use any of them.

You've also got potential browser problems, particularly with IE, which (pre-IE8) does some weird things with the Element.*etAttribute* family of DOM calls. And unless you're actually serving the document as an XML Content-Type (which IE also can't handle), all of your namespace stuff isn't actually using namespaces anyway.

In [X]HTML5 there is a proposal to allow user custom attributes (primarily for scripting purposes) to go in attributes whose names start with ‘data-’. But in the meantime the usual method is to hide values in another attribute, for example class:

<div class="userid-123">...</div>

and then extract the data using suitable string processing over className in script.

like image 36
bobince Avatar answered Nov 10 '22 13:11

bobince


I have a website that a client requires to be XHTML valid. They check it using the W3C validator.

I've had some success with the W3C validator using, not xmlns=, but taking the standard XHTML DTD and adding a few attributes at the end as bobince describes above. I am using this to add attributes used by the PHPTAL templating system, such as

<html metal:define-macro="m">

I agree that what you need is not XHTML but a superset of XHTML. However, if your client is prepared to test simply by pointing the W3C validator at your URL, then go ahead. For advice, see WDG

like image 20
cjakeman Avatar answered Nov 10 '22 13:11

cjakeman