当サイト、Codex 日本語版は今後積極的な更新は行わない予定です。後継となる新ユーザーマニュアルは、https://ja.wordpress.org/support/ にあります。
万が一、当サイトで重大な問題を発見した際などは、フォーラムWordSlack #docs チャンネルでお知らせください。</p>

HTML to XHTML

提供: WordPress Codex 日本語版
2017年7月8日 (土) 21:46時点におけるGblsm (トーク | 投稿記録)による版 (見出しを和訳。本文は「XHTML と HTML の違い」節まで和訳。)

移動先: 案内検索

このページ「HTML to XHTML」は「XHTML の書き方」節以降が未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

XHTML は何であり何でないか

システムとしての WordPress は XHTML スクリプト言語で書かれた文書に基づいています。XHTML 1.0 (現在最も広くサポートされているバージョン。eXtensible Hyper Text Markup Language、拡張可能なハイパーテキストマークアップ言語の略) が W3C の推奨になったのは西暦2000年で、XHTML 2.0 が標準化されるまでの中継ぎとされていました。しかし8年が過ぎても XHTML 2.0 は標準になっていません (訳注: 2009年に策定が打ち切られました)。この文書では XHTML という用語を XHTML 1.0 を指すものとして使います。

XHTML はどちらも SGML と呼ばれた言語の後継である点で HTML にとてもよく似ています。しかし XHTML は、HTML よりもっと厳格な構文規則を持つスクリプト言語 XML の後継でもあり、その規則の一部を受け継いでいます。主に新しい MIME タイプの使用と新しい文法の追加という点で、XHTML は HTML と異なっています。以下にそれらの差異を説明します。

XHTML を使う理由

WordPress はすべての内部関数から XHTML を出力します。そのためすべてのテーマと、大部分のプラグインも同様に XHTML を出力します。ですから WordPress を使いたいなら、今こそが XHTML に取り組み学ぶべき時です。

XHTML と HTML の違い

HTML に慣れているなら、HTML の知識の大部分が XHTML でも通用することにホッとするでしょう。主な違いは、XHTML を使う Web ページ作者は従来よりも一貫性があり読みやすいコードを書く必要がある点です。しかし構文と文法に幾つか違いがあり、一部の HTML タグが廃止された、その程度です。HTML を知っていれば XHTML への乗り換えの容易さに驚くでしょう。そして XHTML の新しい規則はあなたを良いプログラマーに育てるでしょう !

XHTML の書き方

Well, here's a quick check list of the important requirements of XHTML and the differences between it and HTML. This is NOT a comprehensive XHTML language reference. Please look here for that.

In these examples some code has been omitted for clarity

タグ、属性、値をすべて必ず小文字で書く

正しい例:

<a href="www.kilroyjames.co.uk" >

間違った例:

<A HREF="www.kilroyjames.co.uk" >

属性値を必ずクォートで囲む

正しい例:

<a href="www.kilroyjames.co.uk">

間違った例:

<a href=www.kilroyjames.co.uk>

タグの入れ子を正しく

正しい例:

<em>this emphasis just keeps getting <strong>stronger and stronger</strong></em>

間違った例:

<em>this emphasis just keeps getting <strong>stronger and stronger</em></strong>

XHTML 文書に必ず DOCTYPE 定義を付ける

The DOCTYPE is an intimidating looking piece of code that must appear at the start of every XHTML document, it tells the browser how to render the document.

Rules for the DOCTYPE tag:

  • It must be the first tag in the document
  • The DOCTYPE is not actually part of the XHTML document so don't add a closing slash
  • It should point to a valid definition file called a DTD that tells the browser how to read the document
  • You must write the DOCTYPE tag correctly otherwise your document will explode* (into little pieces of HTML called "tag soup") and be unvalidateable.

* I am, of course, perfectly serious...


There are three types of valid XHTML 1.0 document: Strict, Transitional, and Frameset. If you can get your document to validate with "Strict" then do so, however some legacy tags and attributes aren't allowed in Strict so you can use "Transitional" instead.

Note: you might have a problem getting WordPress to validate as Strict because, as of version 2.6.2, some of the internally generated <form> elements still use the "name" attribute, which is not allowed under the Strict DTD, ie. <input name="my_button" />

Note also that using a Transitional DTD takes most browsers out of "Standards" mode. It is much trickier to get your web pages to look consistent across different browsers when the browsers are not in Standards mode. I'm not going to explain the minutiae of the DOCTYPE tag as it gets deeper and more complicated, just know that for best results you should use one of the following, preferably the first one (Strict):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

HTML タグに必ず XMLNS 属性を含める

You don't need to understand the "XML namespace" attribute, except to know that it is required in all XHTML documents. Here is an example of how to write it:

<html xmlns="http://www.w3.org/1999/xhtml">

HTML, HEAD, TITLE および BODY タグを使った適切な形式の文書

In HTML it is possible to write a webpage that contains none of the above tags; in XHTML it is not. The above tags must be included and they must be nested and ordered correctly, as follows (the DOCTYPE has been omitted):

<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title></title>
 </head>
 
 <body>
  <p>   
    See how the TITLE must be placed in the document HEAD – the TITLE is considered 
to be a "required child" element of the HEAD.
Notice that the HEAD must also appear before the document BODY.
Notice also how both the HEAD and the BODY must be contained
within the HTML tag. Again, HEAD and BODY are "required child"
elements of the HTML tag. Finally, notice that this text is
written within a <p>paragraph</p> tag; in XHTML you may
not write text directly in the BODY tag without using a suitable
container tag, such as <p> or <div>. </p> </body> </html>

独立タグでも必ず正しく閉じる

<p>Mary had a little lamb
<p>It's fleece was white as snow

This code is not valid XHTML as the closing </p> tags have been left out. The following example is correct.

<p>Mary had a little lamb</p>
<p>It's fleece was white as snow</p>

In XHTML even single tags have to be closed - absolutely NO tag may be left open.

<p>
 Mary had a little lamb <br>
 It's fleece was white as snow
</p>

Therefore the above example is wrong because the <br> tag is not closed. To close a single tag like <br> and <hr> you simply add a forward slash before the final bracket, like so: <br /> and <hr /> (the white space is optional). To correct the above example we'd write:

<p>
 Mary had a little lamb <br />
 It's fleece was white as snow
</p>

This is correct XHTML.

属性の最小化は禁止

In HTML, attributes can be strung together almost like they were keywords, ie. <dl compact>, this is called attribute minimisation. In XHTML that is not allowed, attributes and values must be explicit, ie.

<dl compact="compact">

ID および NAME 属性

In HTML it was legal to use ID and NAME attributes interchangeably. In XHTML the NAME attribute is formally deprecated and cannot be used. In all cases where you would think to use a NAME attribute you must now use ID instead. e.g.

correct HTML

<input type="submit" name="s" value=" Search " >

and now the correct XHTML version

<input type="submit" id="s" value=" Search " />

STYLE はすべて HEAD に入れる

XHTML does not allow STYLE declarations within the body of a document they must be placed in the document HEAD instead.

エンティティ参照

Write all literal ampersands as &amp; or they will be assumed to be part of an entity reference. e.g. &reg; is the entity reference for the symbol ®. Therefore M&S is invalid XHTML because &S is not an entity reference, you must write it as M&amp;S.

まとめ

As was previously mentioned, this is not a comprehensive reference but it should be enough to get you up and running with XHTML pretty quickly. Good luck!

XHTML の問題点

Most people don't realise that to use XHTML properly it must be served using the new MIME TYPE "application/xhtml+xml". A MIME TYPE is simply a description that the web server sends to a browser to tell it what sort of document is coming. For instance a JPG image is sent with a MIME TYPE of "image/jpeg" and an HTML document with a MIME TYPE of "text/html". Sending an XHTML document with a MIME TYPE of "text/html" results in the document being parsed and validated as HTML, not as you would no doubt hope, as XHTML. You must use the correct MIME TYPE if you want to use XHTML otherwise you are simply using non-standard HTML. In order to avoid this problem and output standards compliant code you can use the XHTML to HTML wordpress plugin.

HTML5

Because of seemingly intractable problems with the development of XHTML (mainly that XHTML 2 is incompatible with previous versions of XHTML and HTML and also the MIME TYPE issue), a competing standard supported by Mozilla (Firefox), Apple (Safari), Opera, Microsoft (Internet Explorer) and some other key Internet players has become the new favourite to succeed the old HTML 4.01 standard.

HTML5 was passed as a working draft by the W3C in January 2008 and became a candidate recommendation in December 2012.

関連項目