DTDs are a basic building block of any valid XML document. In fact, DTDs provide a simple reference grammar to validate our XML documents. A DTD tells to a validating XML parser what kind of content, attributes or elements can actually be contained within another element. In this post I'm going to discuss with you some basic aspects of the DTDs, explaining why they are so useful.
Showing posts with label dtd. Show all posts
XML templates, nomenclature and DTDs
One of the most needed feature of XML concerns the lack of a common DTD to be used as a basic template system for building XML documents. At the time of this post, only DocBook provides such an alternative, though its DTD is not recognized by any browser, so that it simply doesn't exist a default stylesheet used to display a DocBook document (as it happens to MathML). This point is particularly interesting, because many developers have yet noticed that most of the new elements of HTML 5 owe something to the DocBook DTD. So the question is: what should we do?
The first (and most feasible) option is to use XSLT for transforming an XML document into an HTML document, stylize it and display it on a web browser. So far so good. Anyway, this is a more time-consuming approach, because it involves 1) the XML document source 2) the XSLT stylesheet 3) the HTML transformation 4) the CSS styles 5) the final rendering on a web browser. Instead, if we had a common DTD and the resulting shared template system, we'd only need three steps (1, 4, 5).
The second option, that I'd like to propose, is the usage of common naming conventions in order to build a shared XML template system. That is, this is exactly the same thing proposed for CSS by Andy Clarke in his book Transcending CSS. In a nutshell, if web developers used a common nomenclature for certain XML elements, we could build up a shared XML template system that might be reused in many different scenarios, for example with remote services processing or Ajax. This would reduce the likelihood of inconsistencies during the parsing and processing of XML documents. Further, we might even be able to write a shared CSS file used for the rendering of such common elements. Is this utopia? Time will tell.
Building a web site with XML
Building a web site with XML was actually the topic of my latest seminar in Milan (SMAU 2009). During this seminar many people asked me some questions that made me think about the effective reliability of such a solution. Here are some topics:
- Browser support
Versions 6, 7 and 8 of Internet Explorer support XML rendering only when the latest version of MSXML is installed and configured. In other words, these versions of IE don't have a native support for XML. What's more, the rendering of CSS styles is quite inconsistent. Check out some of my tests on this subject at CSS Zibaldone.
- Validation and DTDs
Some people asked me how they could validate their XML documents. Basically, you can do this either with a custom DTD or with an XML Schema. It depends a good deal on the dimensions of your web site and the number of elements and attributes you want to use. Personally I like to write my own DTDs, but sometimes I use some XML Schemas, because this solution is faster and it doesn't force you to learn another syntax or grammar (XML Schemas are basically XML).
- Element names
Coming to XML element names, it's always a good choice to use semantic element names, such as
branding,content-suband the like (see Andy Clarke's thoughts on the subject), and avoid presentational names such asheaderorsidebar. - Using XSLT
For the final rendering of our document structure, the preferred choice is using XSLT. By doing so, you can serve your structure as XHTML and prevent some browser inconsistencies from happening. For example, if you want to use hyperlinks in your documents, using XLink is not the best solution, because the current browser support is really poor.
- Accessibility
Theoretically speaking, if you use XSLT to transform your XML structure into XHTML, browsers should treat your documents accordingly, and recognize your XML elements as XHTML elements. Anyway, I didn't test it with a screen reader, so you should always run some tests before using this solution. If not, screen readers will probably not recognize your elements and some serious accessibility problems will arise.