[转]Validating a Custom DTD
这是一篇关于在HTML和XHTML中使用自定义标签或属性并通过W3C验证的文章。 转自:http://www.alistapart.com/articles/customdtd/ 现在HTML5貌似已经支持自定义属性了。 In his article in this issue, Peter-Paul Koch proposes adding custom attributes to form elements to allow triggers for specialized behaviors. The W3C validator won’t validate a document with these attributes, as they aren’t part of the XHTML specification. This article will show you how to create a custom DTD that will add those custom attributes, and will show you how to validate documents that use those new attributes. Here is a sample of the HTML with the custom attributes that let us specify the maximum length of a text area and whether a form element is required or not:
Name:
Email:
Comments:
[b][size=medium]What’s a DTD?[/size][/b] A Document Type Definition (DTD) is a file that specifies which elements and attributes exist in a markup language and where they can appear. Thus, the XHTML DTD specifies that
is a valid element, and that it can appear inside a
, but not inside a . The URL at the end of your DOCTYPE declaration points to a place where you will find the DTD for the flavor of HTML you’re using. Neither your browser nor the W3C Validator goes out to the web to find the DTD — they have a “wired-in” list of the valid DOCTYPEs and use the URL for identification purposes only. As you will see later, this will change when you make a custom DTD. [b][size=medium]Specifying the attributes[/size][/b] Adding attributes to an existing DTD is easy. For each attribute, you need to specify which element it goes with, what the attribute name is, what type of values it may have, and whether the attribute is optional or not. This information is specified in this model: To add the maxlength attribute to the You’ll get this output from the validator: [Error] badfile.html:12:70: Attribute "required" with value "yes" must have a value from the list "true false ". [Error] badfile.html:14:63: Attribute "inquirer" must be declared for element type "textarea" badfile.html: 611;82;0 ms (15 elems, 20 attrs, 9 spaces, 43 chars) [b][size=medium]Another validation method[/size][/b] If you are using the jEdit editor, you may download the XML plugin. If you name your file with the extension .xhtml, jEdit will validate using your custom DTD as specified in the DOCTYPE. [b][size=medium]Conclusion[/size][/b] It is easy to specify additional attributes for XHTML elements; with a little bit of work, you can set up a validator to check your files against your custom version of HTML. Download all the sample files from this article and give it a whirl. [b][size=medium]About the Author[/size][/b] J. David Eisenberg is a programmer and teacher living in San Jose, CA with his cats, Marco Polo and Big Tony. He has written a book about Scalable Vector Graphics. He is also interested in the OpenDocument Format and foreign language learning.