Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
Exploring XHTML, the dotnet way!
Posted Date: 07 May 2008 Resource Type: Articles Category: Web Applications
|
Posted By: Balamurali Balaji Member Level: Diamond Rating: Points: 10
|
Introduction
Most of the times transporting HTML, XML, CSS or combination of all these mark-ups into devices like PDAs, mobiles is bit more complicated. Some additional resources are always missed out, or, it requires additional HTML mark-up to compensate. You need specialized parsers and user-agents to represent content, 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 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 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 the web content targeting various devices and browsers.
Even though there are many versions of XHTML available and different browsers support different versions, the sample in this article works with XHTML 1.1.
For more information on XHTML, its standards and uses, one has to read at http://en.wikipedia.org/wiki/XHTML and http://www.w3.org/TR/xhtml1/ .
ASP.NET and XHTML
Most of the developers are already familiar with the support of 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 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 XHTML-Conformant
If your HTML or ASPX page does not already contain XHTML-compatible mark-up, you can open 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 Validation drop-down list.
• Be sure that validation is enabled so that you can see errors in 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.
The content of 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> <font color="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"/> </font> </body> </html> </xsl:template> </xsl:stylesheet>
When you browse the info.xml on the 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 COM component
HTML Checker Type Library 1.0 is a COM component; you can add reference to it in your .Net project. It has a class called XMLPProcessorClass used for converting your HTML into XHTML formats and you may also define your rules/specifications you have to meet in your XHTML document. You can convert HTML XML mark-ups into XHTML using the Convert or HTMLtoXHTML method found in this class.
The following piece of code converts an xml into XHTML and saves it in a file with .html extension.
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 the newinfo.html, you will get all transformations removed and both the IE and Safari displays only the content with out transformations as below.
MS Systems Info Way BB avenue Hi-Tech city India
Conclusion From this, it is very clear that XHTML delivers the content what all the browsers accept in common in many cases. But, this is not true when you combine few objects that are non-conformant with HTML, XHTML comes in handy to produce desired results according to the browsers.
In this article, we discussed about the use of XHTML in place of HTML in web pages that can be easily targeted to different devices and browsers.
For more details, visit http://w3.org/tr/xhtml1
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|