Click here to Skip to main content
15,889,096 members
Articles / Web Development / ASP.NET

Exploring XHTML, the .NET way!

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
21 Jun 2009CPOL4 min read 18K   9   2
This article explores the usage of XHTML in a VS.NET environment.

Introduction

Most of the time, transporting HTML, XML, CSS, or a combination of all these mark-ups into devices like PDAs and mobiles is a bit complicated. Some additional resources are always missing, or it would require additional HTML mark-up to compensate. You need specialized parsers and user-agents to represent content and extract data out of it. HTML supporting browsers always have constraints specific to different devices, user-agents, and parsers.

To overcome these limitations in HTML and HTML based browsers, XHTML (Extensible Hypertext Mark-up Language) was introduced way back in 2000. It works to the deepest of levels of expressing content as HTML conforming to XML standards.

The use of XHTML

When a device requires a web page, the page is delivered to it based on the nature of the device, its browsing capability, and its support for markup languages. With XHTML, developers can deliver the content to different devices and browsers without worrying much about user-agents. Most of the things are taken care of by XHTML, and you need not have to check for user-agent strings and then display the content appropriately.

XHTML has become a standard for many browsers including Internet Explorer so as to enable automatic processing of XML, well-formed XML. With different DOCTYPEs and namespaces, XHTML is far too simpler and convenient to present web content targeting various devices and browsers.

There are many versions of XHTML available and different browsers support different versions, the sample in this article works with XHTML 1.1.

More information on XHTML, its standards, and uses can be obtained at http://en.wikipedia.org/wiki/XHTML and http://www.w3.org/TR/xhtml1/.

ASP.NET and XHTML

Most developers are already familiar with the support for different versions of XHTML by Visual Studio. Visual Studio 2008 supports XHTML 1.1, XHTML 1.0 transitional and frameset document types. You may choose one among these formats as mark-up Validation when you edit mark-up in the Source view of the Visual Studio Web designer. The editor continually checks that the mark-up you are creating is valid, like the spelling checker in a word processor.

VS 2008 does not support the XHTML 1.0 Strict Doctype.

VS Web Designer defaults to XHTML 1.0 transitional doctype, and includes the basic elements that are required for XHTML such as DOCTYPE and an html element that includes a reference to the XHTML namespace. During the page execution, ASP.NET performs certain tasks to make the page compatible to XHTML. Those tasks include:

  • Adding an action attribute to the form element.
  • Rendering an HTTP header that includes information about the current character set, encoding, and so on.

Convert web pages to be XHTML-conformant

If your HTML or ASPX page does not already contain XHTML-compatible mark-up, you can open it in VS Web Designer and the default validation settings will flag all of the elements in the page that do not conform to XHTML standards. Visual Studio does not add any missing elements, and it makes only minor corrections to elements, such as adding a closing slash (/) to elements that should be self-closed.

If you want to make a page conformant to XHTML standards, you should do all of the following:

  • Set the browser schema to XHTML 1.0 Transitional, XHTML 1.0 Frameset, or XHTML 1.1. To do this, in the HTML Source Editing toolbar, select a schema from the Target Schema for the Validation drop-down list.
  • Be sure that validation is enabled so that you can see errors in the Source view. To set Validation options for HTML editing in Visual Web Developer, go to Tools ->  Options ->  Expand Text Editor ->  Expand HTML ->  Select Validation ->  choose the XHTML version you want.
  • Test your document by using an XHTML validator, such as the free W3C Mark-up Validation Service, which is maintained by the World Wide Web Consortium and can be obtained on the W3C web site.

To illustrate the power of XHTML in real world applications, let us take an XML file that uses transformation to display data.

XML
<!--info.xml:->
<?xml:stylesheet type="text/xsl" href="d:\bala\infotrans.xsl" ?>
<company>
    <name>MS Systems</name>
    <address1>Info Way</address1>
    <address2>BB avenue</address2>
    <city>Hi-Tech city</city>
    <country>India</country>
</company>

<!--Infotrans.xml-->

<xsl:stylesheet 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">

<xsl:output method="html" />        
    <xsl:template match="/">
        <html>
            <head>
                <title>Welcome to <xsl:value-of select="/company/name"/></title>
                <style>
                    body,td {font-family:Tahoma,Arial; font-size:9pt;}
                </style>
                
            </head>
            
            <body> "blue">
                <h2>Welcome to <xsl:value-of select="/company/name"/></h2>
                <p/>
                <b>Our contact details:</b>
                <br/>
                <br/>        
                <xsl:value-of select="/company/name"/>
                <br/>
                <xsl:value-of select="/company/address1"/>
                <br/>
                <xsl:value-of select="/company/address2"/>
                <br/>
                <xsl:value-of select="/company/city"/>
                <br/>
                <xsl:value-of select="/company/country"/> 
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

When you browse info.xml on Internet Explorer, you will get the following output:

Welcome to MS Systems
Our contact details:

MS Systems
Info Way
BB avenue
Hi-Tech city
India

Obviously, you cannot get the same results in other browsers; in Apple's Safari, I get the following:

MS Systems Info Way BB avenue Hi-Tech city India

XHTML conversion using a COM component

HTML Checker Type Library 1.0 is a COM component; you can add a reference to it in your .NET project. It has a class called XMLPProcessorClass used for converting your HTML into XHTML format, and you may also define the rules/specifications you have to meet in your XHTML document. You can convert the HTML XML mark-ups into XHTML using the Convert or HTMLtoXHTML methods found in this class.

The following piece of code converts an XML into XHTML and saves it in a file with a .html extension:

C#
HTMLCHECKERLib.XMLPProcessorClass xpp = new HTMLCHECKERLib.XMLPProcessorClass();
string Xhtmlstr = xpp.HTMLtoXHTML(System.IO.File.ReadAllText(@"d:\bala\info.xml"));
System.IO.File.WriteAllText(@"d:\bala\newinfo.html", Xhtmlstr);

When you browse newinfo.html, you will get all the transformations removed, and both IE and Safari displays only the content without the transformations, as below:

MS Systems Info Way BB avenue Hi-Tech city India

Conclusion

From this, it is very clear that XHTML delivers content that all browsers accept in common, in most cases. But, this is not true when you combine a few objects that are non-conformant with HTML. XHTML comes in handy to produce desired results depending on the browser.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder BB Systems CIT-GPNP
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
GJ.Coder22-Jun-09 8:12
GJ.Coder22-Jun-09 8:12 
poor
GeneralMy vote of 1 Pin
zlezj22-Jun-09 2:03
zlezj22-Jun-09 2:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.