Sunday, February 06, 2005

Custom tags, namespaces and DTDs

When you want to add extra functionality to web applications, you might want to add extra attributes or tags. The idea of custom attributes is presented in a nice article by Peter-Paul Koch on A List Apart: JavaScript Triggers, and further explained on PPK's website.

In the article below I give some extra ideas about custom tags and attributes, and how they can be combined with namespaces and DTDs.

Namespaces and DTDs have a different purpose. Namespaces make it possible to mix different XML vocabularies in a single document. DTDs make it possible to check the validity of an XML document.

If you use a custom attribute ‘required’ in an XHTML document, you are basically mixing your own XML-language with XHTML. There could be naming conflicts if a future version of XHTML also introduces the ‘required’ attribute. Therefore it’s better to put your own tags and attributes in your own namespace. You indicate the namespace with a namespace prefix. This could be the result:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "" >

<html xmlns="" xmlns:ppk="" >

<textarea ppk:required="true"></textarea>

Then you might want to check for validity, both for XHTML and for your own attributes. Checking for XHTML validity is important to ensure compatibility with web browsers. You cannot change web browser implementations, so you better make sure your documents are compliant, otherwise your pages might be rendered incorrectly. Your own XML tags and attributes will NOT validate based on the standard XHTML DTD, regardless whether they are in their own namespace or not. But the good thing is that the browser also won’t mind about these extra tags: they’re just ignored (1).

So it’s your own responsibility to make sure that your own tags and attributes are processed correctly, using JavaScript. Then it makes sense to validate your custom markup, otherwise your JavaScript might not work correctly. For this purpose you can add your own tags and attributes to the DTD. For validation it is NOT important whether you use namespaces or not. A validating DTD parser is not even aware of namespaces (see Roland Bourret's Namespace FAQ). Validation is helpful during authoring of documents, and many authoring environments can use DTDs or Schemas for code completion and validation.

At Backbase we have introduced two additional namespaces (one for visible tags, and one for system tags), and we have implemented code completion and validation for DreamWeaver, Eclipse and Visual Studio.NET.

(1) NOTE: if you use a namespace, you have to declare it, otherwise some browsers (such as Mozilla 1.8) will display XML errors. In the example above I have declared the namespaces in the HTML tag.


Anonymous Keenan Brock said...

I've been trying to tackle the same problem and enjoyed your article.

When I ran your sample application through the w3 validator, it says it is not valid

Any ideas?


11:06 AM  
Blogger Jep Castelein said...

The W3C validator is hard-coded to check for (X)HTML, and is not a generic XML checker. It also isn't aware of name spaces. Therefore it doesn't recognize the Backbase XML tags (BXML), not the Backbase name spaces. A generic XML validator will work.

11:12 AM  
Anonymous Keenan Brock said...

Thanks Jep,

After reading the articles at alistapart, some of which I had read before coming to your site, I realized that I asked a FAQ.

Thanks for the kind response and a blog with some nice articles.

8:07 PM  

Post a Comment

<< Home