When is an xmlns declaration in HTML5 really necessary [duplicate] - html

This question already has answers here:
What does "xmlns" in XML mean?
(5 answers)
Closed 7 years ago.
I'm trying out different web page compositors and most of them start out with a basic structure like this when I create a new project:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Web Project</title>
</head>
<body>
<h1>New Web Project Page</h1>
</body>
</html>
I wasn't really able to find an answer as to why there needs to be an xmlns if it's going to be a normal web page. I know I can omit it if I so desire, I've been writing HTML5 documents before and it was working fine without it.
So when is it actually necessary to provide an xmlns in the <html> element and why do compositors think it should be there when I create a new project? Is there any significance providing an xmlns with the <html> tag when doing HTML5 in the first place? Is there any benefit adding it?

On a regular HTML5 page you don't need it. However, if you want XML-serialized HTML5 (i.e. XHTML) then you add the XML namespace. For example the web framework JSF uses XML-serialized HTML, so there would be one reason to use it.

The HTML validator at http://w3.org does not complain when the xmlns attribute is missing in an XHTML document. This is because the namespace "xmlns=http://www.w3.org/1999/xhtml" is default, and will be added to the tag even if you do not include it.
From http://www.w3schools.com/tags/att_html_xmlns.asp

Related

HTML5 Doctype with strict

I want a strict but fully compatible html5 alternative to:
<!doctype html>
Basically I want to ensure the use of closing tags just to keep everything well readable, consistent and highlighted clearly in editors.
The answer to this question is to HTML 5, as XHTML-1.0-strict is to HTML 4.
Thanks in advance.
There is no doctype for "strict" XHTML5 validation. For XHTML5 the doctype is even optional, as the doctype is only for stopping the browser to switch to quirksmode. There is no such quirksmode for XHTML. It is recommended to use the HTML5 doctype (with capitalised DOCTYPE) if you are planning to use it as a polyglot document. In that case you would use the doctype:
<!DOCTYPE html>
However, if you want to validate as if the document is using XHTML style syntax, you can achieve that using the advanced options of the validator.
Go to http://validator.nu
Switch to "text field" in the select box (or point it to your online document but make sure it is served as XHTML not text/html
If using the text field paste in your document. In my case I used the following:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="UTF-8" />
</head>
<body>
<p>test
</body>
</html>
Select XHTML5 + SVG 1.1 + MathML 3.0 from the Preset field. This will pre fill the scheme as http://s.validator.nu/xhtml5.rnc http://s.validator.nu/html5/assertions.sch http://c.validator.nu/all/
Click Validate
Using my document it will warn about the missing close </p>.

jsf 2 ui:include .html give error "XML Parsing Error: no element found"

I include a header.html (not .xhtml) to my page, but when I preview my page, it give me error "XML Parsing Error: no element found". I do know that it is the tag no properly closed issue, but since my header page is a html file, not a xhtml file, it shouldn't must close the tag, am I right?
If I do close all the meta tag in my header.html, my page is working fine, but I wish to know that is it a must close all the tag properly in html file if I include them in jsf2, thanks.
header.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Header</title>
<meta name="description" content="">
</head>
<body>header content</body>
</html>
welcome1.xhtml
<ui:include src="header.html" />
<p>welcome page</p>
From Oracle's documentation, <ui:include> is used to encapsulate and reuse content among multiple XHTML pages. The src attribute is expected to point to a well-formed XML document. Even if you want to include .html pages, you need to make sure all tags are closed properly.
If you look more closely, you will see the closing <html> tag in you header.html, which should end the HTML document, as you can see in Structure of an HTML document. This way, when you include the file in the final view you break that contract.
In JSF you'd be better off using the templating option of facelets. For example in a master template you can make insert points, like the scripts section, and later define inserted contents in template client pages (final views). Kickoff example can be found in this tutorial by mkyong.

Check If All Tags Are Closed

I have a large dynamic website that is being constructed by PHP. I suspect that one of my components are not closing the HTML tags properly. I have the source output HTML. I am wondering if there is a script, or website, that will tell me if all my tags are closed and such?
Use the W3C Markup Validator Service at http://validator.w3.org/
Just for interest, I shall point out that validator.nu is better for checking HTML4 than the W3C validator. Suppose your markup is this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="Test" /
<title>Test Case</title>
</head>
<body>
<p></p>
</body>
</html>
The <meta> element clearly isn't closed where it should be, and the result is that the <title> element won't be recognised in browsers.
But give that markup to the W3C validator and it will tell you that it validates. That's because it is based on SGML processing which permits a syntax known a Null End Tag (NET) syntax, which makes it think that the / ends the tag.
Browsers do not support NET syntax, and neither does validator.nu, thus correctly flagging the markup as in error.
For HTML5, both validators are good.
Try HTML Tidy. Theres a firefox plugin version as well
If you work alot in your browser and dont want to jump back and forth to the w3 schools this is a good choice, but like everyone who commented said, the validator is good as well.
This Website checks if there are unclosed tags, and tells you which ones are not closed, and the line they belong

Quest for Minimalistic DTD HTML Language Declaration Standards

In a search towards minimalistic webcoding practices I saw this on the top of my webpages:
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="nl" xml:lang="nl">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="nl"/>
A: when should one keep the second line (xmlns) and when can it be removed?
B: my website is multilingual so pages come in variety of languages. I kept the xmlns since I swa you can put in lang= and xml:lang= so thought that might be handy, but I don't use xml I think... just php generated contents etc. UPDATE I just got advise to use <html lang="de"> which will suffice for me. True?
C: when does one need the third line http-equiv? When can it be removed entirely?
D: do browsers recognize/process the fourth line or do they skip it nowadays?
Thanks very much!
If you haven't already, read up on HTML5 here or maybe here, and maybe even here. HTML5 is the latest HTML spec currently under development by the W3C, and is pretty much awesome.
For your questions:
Q1: See this SO question. Basically, you won't need the xmlns or xml:lang attribute.
Q2: Yes, see below.
Q3: You don't need to specify content-type anymore, however you should always specify the encoding (see below). In reality, though, you can use either one, so I would just stick with the short version. See this SO question for more info.
Q4: That's a difficult question to answer because it really depends on the browser / version. However, it's a moot point since the lang attribute should really be moved into the html tag (again, see below).
Below is what I consider the bare minimum a proper web page should include, (sans the comments):
<!DOCTYPE html>
<html lang="en"> <!-- use whatever language code is appropriate here -->
<head>
<meta charset="utf-8"> <!-- utf-8 is universally the best encoding option -->
<title>My Cool Website</title>
</head>
<body>
</body>
</html>

Is it ok to remove the lines below in a HTML file?

Is it ok not to include such lines in a HTML file? Removing these lines makes the code look more clean.
<!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/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
When I use dreamweaver to create a HTML file, these lines are automatically included.
No, you should NOT remove those lines.
You can, however, switch the <!doctype>-declaration to the one of HTML5, since that will still trigger standards mode in all current browsers, even though they don't yet implement HTML 5. It looks like the following:
<!DOCTYPE html>
Which is a bit more clean than the ordinary one you use looks like. You can also read a little more about the new doctype-declaration here. You can also learn more about what will change in HTML5 here.
No that is not allowed. It sets the character encoding, i.e. how the browser should read it.
But the new HTML5 elements make the whole thing look easier and cleaner. So:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
becomes --> <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
lang="en"
xml:lang="en">
becomes --> <html lang="en">
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
becomes --> <meta charset="UTF-8">
<link rel="stylesheet" href="style.css" type="text/css">
becomes --> <link rel="stylesheet" href="style.css">
So the whole code in the <head> becomes:
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
No, you shouldn't remove these lines. The first two lines tell the browser what type of document your page is, and helps the browser render it properly.
The third line tells the browser what character set you're using, in this case so that it knows to render non-latin characters properly.
Those declares the DOCTYPE, which shouldn't be forgotten to add.
Why?
Why specify a doctype? Because it
defines which version of (X)HTML your
document is actually using, and this
is a critical piece of information
needed by browsers or other tools
processing the document.
For example, specifying the doctype of
your document allows you to use tools
such as the Markup Validator to check
the syntax of your (X)HTML (and hence
discovers errors that may affect the
way your page is rendered by various
browsers). Such tools won't be able to
work if they do not know what kind of
document you are using.
But the most important thing is that
with most families of browsers, a
doctype declaration will make a lot of
guessing unnecessary, and will thus
trigger a "standard" parsing mode,
where the understanding (and, as a
result, the display) of the document
is not only faster, it is also
consistent and free of any bad
surprise that documents without
doctype will create.
You can remove the DOCTYPE, html tags and meta tags and still have valid HTML, and if you are happy for your page to take browser default styling they can be safely omitted. The content type and charset can be specified by the HTTP headers if you prefer. As others have already pointed out, the DOCTYPE will affect how styling instructions are interpreted, and also how HTML parsers interpret some invalid markup, so you will need to allow for this.