Learn How to Properly Format Your HTML Documents - Including DTD's, Character Encoding, and Namespaces (Part 2)

By: Daniel Imbellino
Updated: Feb 28, 2013

On the last page we discussed document type declarations, which we show examples of their use on this page. We will also look at "namespaces" and "mediatypes", and dicuss why you need to know about them, as well as how to work with them.

On the last page we looked at "doctypes" and dicussed when they should be used, but here we're going to show a few examples of their use in the real world.

What if we wanted to format a webpage as xhtml 1.0 Transitional, how would we do this?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999" xml:lang="en-US" lang="en-US">
<meta http-equiv="Content-Type" Content="text/html; charset="UTF-8" />
<title>title goes here</title>
<style type="text/css">
(Style info goes here)

Notice the "<meta http-equiv="Content-Type" content="text/html; charset="UTF-8" />" statement.
Meta tags simply provide information about the document. You can read more about meta tags under our HTML reference main page. The meta "http-equiv" attribute adds an HTTP header to our document, specifies what type of content the document consists of, as well as what character encoding scheme is being used in the document. The "Content-Type" attribute in this document specifies the content as being "text/html", which is called a "media type", as specified by the W3C.

The "XHTML 1.0 Transitional" form of XHTML is exactly that, transitional. It carries the strength of HTML as an XML application, while still providing backwards compatibility with HTML 4.01. If you are using the "Transitional" format of XHTML 1.0, then you will need to use the "text/html" attribute as your "media type". The "text/html" attribute should only be used for HTML and HTML compatible web pages, not for "Strict" XHTML 1.0 type pages.

If you are using the "Strict" document type declaration in your web page, then you will need to use the "application/xhtml+xml" media type as your "meta http-equiv" content type, like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">

You must use the media type "application/xhtml+xml" if you are creating web pages using the "Strict" doctype. The "text/html" media type is not true XHTML, and will not render as XHTML, and cannot be used with this type of XHTML. That being said, the "text/html" attribute should, as said previously, only be used with the "Transitional" XHTML, and HTML doctypes.

If you specify your document type declaration as HTML 4.01, then you must use the "text/html" media type in your "meta http-equiv" statement, just as with the transitional form of XHTML.

What is a "namespace"? Namespaces are used to point to xml schemas that contain the elements, rules, attributes, and attribute values, that are associated with a particular xml language. A "namespace" typically consists of a URL (uniform resource locator) that points to an XML schema that contains rules for the particular language. "Namespaces" are also used for distinguishing between similar elements that exist in separate XML languages and schemas. For instance, you could have two different schemas with the same elements, but the elements have completely different property, attribute, and value rules associated with them. All being said, a "namespace" is simply a collection of elements and their related attributes, and values.

Here is the "namespace" that is used with XHTML 1.0:
<html xmlns="http://www.w3.org/1999/xhtml">
The "xmlns" means "XML namespace", which always consists of a unique URL.