views:

1205

answers:

3

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"&gt;

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?

+3  A: 

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.

kmkaplan
+3  A: 

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.

bobince
Yes, hiding the values in the class attribute is my fallback solution, but obviously it's not very elegant and I wanted something a bit less "dirty". The most elegant solution is the attribute names that start with data- like you mentioned, but looks like for now I'll have to wait for this :-)
Alex York
+1  A: 

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

cjakeman
http://www.alistapart.com/articles/customdtd/If you run such a file through the W3C validator, you find that it validates wonderfully well.... Unfortunately, when you display the file in a browser, the ]> shows up on the screen. There’s no way around this bug, so this approach is right out.
davidcl
But the article goes on to say "An approach that *does work* requires you to obtain the XHTML DTD and add your modifications to that file."It works for me on the W3C validator and also the WDG validator.
cjakeman