bestbuildpc: Forums
 

 Forum FAQForum FAQ   SearchSearch   UsergroupsUsergroups   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Syntactical differences between HTML vs XHTML
 
 
Post new topic   Reply to topic    bestbuildpc Forum Index -> Web Development
View previous topic :: View next topic  
Author Message
bestbuildpc
Site Admin
Site Admin


Joined: Jun 30, 2012
Posts: 213
Location: NL

PostPosted: Wed Jan 02, 2013 0:20    Post subject: Syntactical differences between HTML vs XHTML Reply with quote

Syntactical differences

Document Type Declaration

Let's start with the DTD, the Document Type Declaration, referencing a Document Type Definition (yes, also called DTD, to add to confusion):

Code:
HTML4:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
...
</html>

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


Closing tags & empty elements

Now, that was easy. What might take a bit more of getting-used to is to close what once were optional end tags. Take the <p>-Element, for instance:

Code:
HTML4:

<p>This is the first paragraph ...
<p>And this is the second paragraph

XHTML1:
<p>This is the first paragraph ...</p>
<p>And this is the second paragraph</p>



Why is this important in XML? The answer is easy: the document tree can be built without knowing the document type. That is, wether or not the closing tag is optional and if the element can contain content. For example in HTML, if you didn't know what a <br>-Element was, you didn't know if it needed to be closed or could contain other tags (and as a result, if the following elements would be child elements of the br, or siblings).
In XML/ XHTML, this becomes obvious from the document itself:


Code:
HTML4:

<p>This is the first paragraph ... and here's a break.<br>
And this is the second line</p>

XHTML1:
<p>This is the first paragraph ... and here's a break.<br />
And this is the second line</p>


As you can see, there's an additional spacing and slash ("/") following the br. The spacing is just to not confuse older browsers that don't know XML/ XHTML. The "/"-character tells the browser this element is empty.
Lower-case/ Upper-case

In HTML, you could write element names in upper- or in lower-case letters, and they would still be the same. In XHTML, there is a difference between those two ways of writing it, because XML is case-sensitive. The creators of XHTML decided on lower-case because they had to decide on one thing, and so now XHTML elements and attributes have to be written all lower-case:


Code:
HTML4:

<ADDRESS ID="company">This is an address.</ADDRESS>

XHTML1:
<address id="company">This is an address.</address>


Omitted attribute names

In HTML4, one could use an attribute-name-value shortcut and simply omit the attribute-name. In XHTML1, attributes must always appear in full attribute-value-pair form. Also, the quotations are always necessary (as single, or double quotes), not just sometimes as it was the case with HTML4:


Code:
HTML4:

<option name=usa selected>USA</option>
<option name=germany>Germany</option>

XHTML1:
<option name="usa" selected="selected">USA</option>
<option name="germany">Germany</option>


XHTML is the same as HTML, except it has a cleaner syntax. XHTML uses the same tags as HTML, so people who know HTML know messy XHTML.

Code:
Some new rules are followed in XHTML like:


    XHTML elements must be properly nested.
    XHTML elements must always be closed.
    XHTML elements must be in lowercase.
    XHTML documents must have one root element.
    In HTML, some elements can be improperly nested within each other, like this:

                 <b><i>This text is bold and italic</b></i>

    In XHTML, all elements must be properly nested within each other, like this:

                <b><i>This text is bold and italic</i></b>
  
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:       
Post new topic   Reply to topic    bestbuildpc Forum Index -> Web Development All times are GMT + 2 Hours
 
 Page 1 of 1

 

Jump to:   
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Powered by phpBB © 2001-2008 phpBB Group
 
Forums ©