XML 宣言の省略と XHTML Media Types の Second Edition についてのまとめ

http://hxxk.jp/2008/09/30/0015

記事データ

投稿者

真琴

投稿日時

2008-09-30T00:15+09:00

タグ
概要

XML 宣言を省略する記述方法についての問題提起と、 XML の仕様から見た XML 宣言の定義、 XHTML Media Types の Second Edition と前の Edition との違いなどから、 XML 宣言の省略の機会増までの考察。

リプライ

1 件のリプライがあります。

記事本文

IE 6 では XML 宣言を書くと後方互換モードでレンダリングされる→ XML 宣言を省略するソリューション

昨日 DOCTYPE スイッチについてのまとめと一覧表 (HTML 5 や IE 8 Beta 2 のモードスイッチなどの情報も含んだ 2008 年版 ) にて DOCTYPE スイッチについてまとめましたが、 IE 6 では XML 宣言が書かれていると後方互換モードでレンダリングされるというバグがあるため、 IE 6 では XML 宣言を書かないようにして標準準拠モードでレンダリングさせる、という対処が一般的になっていました。

しかし、 XML 1.0 の 2.8 Prolog and Document Type Declaration では、 XML documents SHOULD begin with an XML declaration which specifies the version of XML being used. と、 XML 1.0 文書は XML 宣言で始まるべきであるとされています。 (XML 1.1 文書の場合は XML 1.1 documents MUST begin with an XML declaration which specifies the version of XML being used. と、 XML 宣言で始まらなければならないとされています。 ) IE 6 がバグによってレンダリングモードが切り替わってしまうから、という理由だけで安易に XML 宣言を書かない、という対処をして良いものなのでしょうか。

XML の仕様から XML 宣言を紐解く

さて、XML 宣言を省略できる条件、というのは実は仕様には書かれてありません。 表現の問題でもありますが、「このような条件の場合に省略できる」というよりは、「省略した場合はこのように扱われる」ということだけが示されています。

XML 宣言がどのような構造になっているか見てみましょう。 XML 1.0 の XMLDecl の部分では、 XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' と示されています。 EncodingDecl( 文字エンコーディング宣言 ) と SDDecl( スタンドアロン文書宣言 ) には ? が付いていますので、ひとつだけ記述するか、あるいは記述しないということになります。

まとめます。 XML 宣言を省略すると、その文書は

という文書であるとして取り扱われます。 これを更にまとめると、文字エンコーディングが UTF-8 か UTF-16 か、またはそれ以外の文字コードであればハイレベルなプロトコルで明示されている XHTML 文書であれば XML 宣言が省略されていてもまあ何とか OK かな、ということです。 ( 省略条件が揃っていても、 XML documents SHOULD begin with an XML declaration which specifies the version of XML being used. ということには変わりありませんので。 )

XHTML Media Types の Second Edition の登場

......と、これまではそういった話でまとまっていました。 ところが、最近は DOCTYPE スイッチによる理由以外でも XML 宣言の記述の有無について考慮する動きが起こっています。

XML宣言とDOCTYPE宣言 - vantguarde - web:g に詳しく書いてありますが、 XML宣言とかPIとか書かないように というガイドラインが示されています。

更に遡ると、 XHTML Media Types (2nd Ed.) - vantguarde - web:gXHTML Media Types改訂の動き | Web標準Blog | ミツエーリンクスというリソースに行き当たります。 これを元に考えを深めていくと、 XML 宣言以外にも変更点があるようです。

なお、 XHTML Media Types の Second Edition は現時点では Editor's Draft であり、 W3C 公式の仕様ではありませんが、初版の XHTML Media TypesW3C Notes ( 現在での Working Group Notes) という位置付けであり、 XHTML Document Development Area の XHTML Media Types を見ても、 Last Public Draft None という状態なので、 Editor's Draft ではありますが新しい仕様を参考にします。

XML宣言とDOCTYPE宣言 - vantguarde - web:g で参照しているのは XHTML Media Types - Second Edition (Editor's Draft 23 April 2008) ですが、この記事の時点では新たに XHTML Media Types - Second Edition (Editor's Draft 27 August 2008) が公開されています。 ( http://www.w3.org/MarkUp/Drafts/#xhtmlmime を参照すると良いですよ、と id:vantguarde さんに教えていただきました。 )

XHTML Media Types の First EditionSecond Edition (Editor's Draft 27 August 2008) の違い (1)

それでは、 XHTML Media Types の First EditionSecond Edition (Editor's Draft 27 August 2008) の違いを考えてみます。 まず目に付く違いとして、 XHTML の種類によって選択する Media Types が違っています。

まずは First Edition の 3.5. Summary の表です。

Media type HTML 4 XHTML 1.0 (HTML 互換 ) XHTML 1.0 ( その他 ) XHTML Basic / 1.1 XHTML+MathML
text/html 推奨 可能 非推奨 非推奨 非推奨
application/xhtml+xml 禁止 推奨 推奨 推奨 推奨
application/xml 禁止 可能 可能 可能 可能
text/xml 禁止 可能 可能 可能 可能

その表が、 Second Edition (Editor's Draft 27 August 2008) の 3.5. Summary では次のように変わっています。

Media type HTML 4 XHTML ファミリ文書型 (HTML 互換 ) XHTML ファミリ文書型 ( その他 ) XHTML ファミリ文書型 + 拡張
text/html 推奨 可能 非推奨 非推奨
application/xhtml+xml 禁止 可能 推奨 推奨
application/xml 禁止 可能 可能 可能
text/xml 禁止 可能 可能 可能

First Edition では、 XHTML 1.0 (First Edition) の Appendix C. HTML Compatibility Guidelines に沿った XHTML 1.0 であれば text/html の指定が可能application/xhtml+xml の指定を推奨ということだったのですが、 Second Edition (Editor's Draft 27 August 2008) では同一文書内の Appendix A. Compatibility Guidelines に沿った XHTML ファミリ文書型であれば text/html の指定が可能application/xhtml+xml の指定が可能というように変わっています。

これまでは XHTML Media Types First Edition を参考にするならば、 text/html で XHTML 文書を公開する場合に XHTML 1.0 (First Edition) の Appendix C. HTML Compatibility Guidelines に沿った上で、かつ XHTML 1.0 を選択すると示されていたのに対し、 Second Edition (Editor's Draft 27 August 2008) などの Second Edition を参考にするならば Appendix A. Compatibility Guidelines に沿っていれば XHTML Family document type であれば良い、という変更です。

視点を変えると、これまでは XHTML 1.1 では text/html は非推奨で、 application/xhtml+xml が推奨とされていたため、 application/xhtml+xml の表示に対応していない IE に対して XHTML 1.1 の使用が躊躇われていました。 これは text/html と XHTML 1.1 でも触れていた点でもありますし、 XHTML Media Types (2nd Ed.) - vantguarde - web:g でも (ガイドラインに沿えば)XHTML 1.1をある意味「合法的」にIEで表示できるようになりそうです とのコメントが述べられています。

XHTML Media Types の First EditionSecond Edition (Editor's Draft 27 August 2008) の違い (2)

前項では Media Types の違いについて触れましたが、それに合わせて参照するガイドラインも変更になっています。 それは次項にまとめるとして、他に小さな相違点を。

application/xml や application/xhtml+xml として文書を公開する場合の meta http-equiv の指定

First Edition では Note that a meta http-equiv statement will not be recognized by XML processors, and authors SHOULD NOT include such a statement in an XHTML document served as 'application/xml' (and 'application/xhtml+xml' as well for that matter). のように、「 XML プロセサは <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> のような指定を解釈しないこと、よって application/xml および application/xhtml+xml として提供される XHTML 文書に対してそのような指定を行うべきでない」と示されていました。

Second Edition (Editor's Draft 27 August 2008) では Note that a meta http-equiv statement will not be recognized by XML processors, and while authors MAY include such a statement a statement in an XHTML document served as 'application/xml' it will not effect processing of the document since the higher level protocol and the XML PI both take precedence. と、「 XML プロセサは <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> のような指定を解釈しないこと、文書作成者はそのような指定を今なおしてくるかもしれないが、よりハイレベルのプロトコルや XML 処理命令が優先される」 と変更されています。

application/xhtml+xml として文書を公開する場合の xml-stylesheet 処理命令

First Edition では When serving an XHTML document with this media type, authors SHOULD include the XML stylesheet processing instruction [XMLstyle] to associate style sheets. のように、「 application/xhtml+xml を指定する場合は、文書作成者はスタイルシートを関連付けるために xml-stylesheet 処理命令を含むべきである」と示されていました。

Second Edition (Editor's Draft 27 August 2008) では When serving an XHTML document with this media type, authors MAY include the XML stylesheet processing instruction [XMLstyle] to associate style sheets. のように、「 application/xhtml+xml を指定する場合は、文書作成者はスタイルシートを関連付けるために xml-stylesheet 処理命令を含んでもよい」 と変更されています。

XHTML Media Types の First EditionSecond Edition (Editor's Draft 27 August 2008) の違い (3)

さて、いよいよ互換性ガイドラインの変更点です。 最初からここだけまとめておけばよかったような気がしてきました。 この部分は訳文を用意する元気がありません......。

Media type XHTML 1.0 (First Edition) の Appendix C. HTML Compatibility Guidelines XHTML Media Types Second Editon (Editor's Draft 27 August 2008) の Appendix A. Compatibility Guidelines
C.1 Processing Instructions → A.1. Processing Instructions and the XML Declaration

Be aware that processing instructions are rendered on some user agents. However, also note that when the XML declaration is not included in a document, the document can only use the default character encodings UTF-8 or UTF-16.

DO NOT include XML processing instructions NOR the XML declaration.

Rationale: Some HTML user agents render XML processing instructions. Also, some user agents interpret the XML declaration to mean that the document is unrecognized XML rather than HTML. Such user agents may not render the document as expected. For compatibility with these types of HTML browsers, you should avoid using processing instructions and XML declarations.

Consequence: Remember, however, that when the XML declaration is not included in a document, AND the character encoding is not specified by a higher level protocol such as HTTP, the document can only use the default character encodings UTF-8 or UTF-16. See, however, guideline 9 below.

C.2 Empty Elements → A.2. Elements that can never have content

Include a space before the trailing / and > of empty elements, e.g. <br />, <hr /> and <img src="karen.jpg" alt="Karen" />. Also, use the minimized tag syntax for empty elements, e.g. <br />, as the alternative syntax <br></br> allowed by XML gives uncertain results in many existing user agents.

If an element has an EMPTY content model DO use the minimized tag syntax permitted by XML (e.g., <br />). DO NOT use the alternative syntax (e.g., <br></br>) allowed by XML, since this may be unsupported by HTML user agents. Also, DO include a space before the trailing / and >.

Empty elements in the XHTML family include: area, base, basefont, br, col, hr, img, input, isindex, link, meta, and param.

Rationale: HTML user agents ignore the  /> at the end of a tag, but without it they may incorrectly parse the tag or its attributes. HTML user agents also may not recognize the alternate syntax permitted by XML.

C.3 Element Minimization and Empty Element Content → A.3. Elements that have no content

Given an empty instance of an element whose content model is not EMPTY (for example, an empty title or paragraph) do not use the minimized form (e.g. use <p> </p> and not <p />).

If an element permits content (e.g., the p element) but an instance of that element has no content (e.g., an empty paragraph), DO NOT use the "minimized" tag syntax (e.g., <p />).

Rationale: HTML user agents may give uncertain results when using the the minimized syntax permitted by XML when an element has no content.

C.4 Embedded Style Sheets and Scripts → A.4. Embedded Style Sheets and Scripts

Use external style sheets if your style sheet uses < or & or ]]> or --. Use external scripts if your script uses < or & or ]]> or --. Note that XML parsers are permitted to silently remove the contents of comments. Therefore, the historical practice of "hiding" scripts and style sheets within comments to make the documents backward compatible is likely to not work as expected in XML-based implementations.

DO use external style sheets if your style sheet uses < or & or ]]> or --. DO NOT use an internal stylesheet if the style rules contain any of the above characters.

DO use external scripts if your script uses < or & or ]]> or --. DO NOT embed a script in a document if it contains any of these characters.

Rationale: XML parsers are permitted to silently remove the contents of comments. Therefore, the historical practice of "hiding" scripts and style sheets within "comments" to make the documents backward compatible may not work as expected in XML-based user agents.

@@@@Put a real example in here that works, and one that does not work@@@@

C.5 Line Breaks within Attribute Values → A.5. Line Breaks within Attribute Values

Avoid line breaks and multiple whitespace characters within attribute values. These are handled inconsistently by user agents.

DO ensure that attribute values are on a single line and only use single whitespace characters. DO NOT use line breaks and multiple consecutive white space characters within attribute values.

Rationale: These are handled inconsistently by user agents.

C.6 Isindex →

Don't include more than one isindex element in the document head. The isindex element is deprecated in favor of the input element.

 
C.7 The lang and xml:lang Attributes → A.7. The lang and xml:lang Attributes

Use both the lang and xml:lang attributes when specifying the language of an element. The value of the xml:lang attribute takes precedence.

DO use both lang and xml:lang attributes when specifying the language of an element in markup languages that support the use of both.

DO NOT use the only the lang attribute, even in languages that include it such as XHTML 1.0.

Rationale: HTML 4 documents use the lang attribute to identify the language of an element. XML documents use the xml:lang attribute. CSS has a "lang" pseudo selector that automatically uses the appropriate attribute depending on the document type. Therefore, specifying both attributes ensures that single CSS selectors will work in both modes.

C.8 Fragment Identifiers → A.8. Fragment Identifiers

In XML, URIs [RFC2396] that end with fragment identifiers of the form "#foo" do not refer to elements with an attribute name="foo"; rather, they refer to elements with an attribute defined to be of type ID, e.g., the id attribute in HTML 4. Many existing HTML clients don't support the use of ID-type attributes in this way, so identical values may be supplied for both of these attributes to ensure maximum forward and backward compatibility (e.g., <a id="foo" name="foo">...</a>).

Further, since the set of legal values for attributes of type ID is much smaller than for those of type CDATA, the type of the name attribute has been changed to NMTOKEN. This attribute is constrained such that it can only have the same values as type ID, or as the Name production in XML 1.0 Section 2.5, production 5. Unfortunately, this constraint cannot be expressed in the XHTML 1.0 DTDs. Because of this change, care must be taken when converting existing HTML documents. The values of these attributes must be unique within the document, valid, and any references to these fragment identifiers (both internal and external) must be updated should the values be changed during conversion.

Finally, note that XHTML 1.0 has deprecated the name attribute of the a, applet, form, frame, iframe, img, and map elements, and it will be removed from XHTML in subsequent versions.

DO use the id attribute to identify elements.

DO ensure that the values used for the id attribute are limited to the pattern [A-Za-z][A-Za-z0-9:_.-]*.

DO NOT use the name attribute to identify elements, even in languages that permit the use of name such as XHTML 1.0.

Rationale: In HTML 3.2 and earlier the name attribute on some elements could be used to define an anchor, but HTML 4 introduced the id attribute. In an XML dialect, only attributes with type ID are permitted to be used as anchors, and the id attribute is defined to be of type ID. Relying upon the id attribute as an anchor will work well in modern HTML and XHTML-aware user agents.

C.9 Character Encoding → A.9. Character Encoding

To specify a character encoding in the document, use both the encoding attribute specification on the xml declaration (e.g. <?xml version="1.0" encoding="EUC-JP"?>) and a meta http-equiv statement (e.g. <meta http-equiv="Content-type" content='text/html; charset="EUC-JP"' />). The value of the encoding attribute of the xml processing instruction takes precedence.

DO encode your document in UTF-8 or UTF-16. When delivering the document from a server, DO set the character encoding for a document via the charset parameter of the HTTP Content-Type header. When not delivering the document from a server, DO set the encoding via a "meta http-equiv" statement in the document (e.g., <meta http-equiv="Content-Type" content="text/html; charset=EUC-JP" />). However, note that doing so will explicitly bind the document to an a single content type.

Rationale: Since these guidelines already recommend that documents NOT contain the XML declaration, setting the encoding via the HTTP header is the only reliable mechanism compatible with HTML and XML user agents. When that mechanism is not available, the only portable fallback is the "meta http-equiv" statement.

C.10 Boolean Attributes → A.10. Boolean Attributes

Some HTML user agents are unable to interpret boolean attributes when these appear in their full (non-minimized) form, as required by XML 1.0. Note this problem doesn't affect user agents compliant with HTML 4. The following attributes are involved: compact, nowrap, ismap, declare, noshade, checked, disabled, readonly, multiple, selected, noresize, defer.

DO use the full form for boolean attributes, as required by XML (e.g., disabled="disabled"). Such attributes include: compact, nowrap, ismap, declare, noshade, checked, disabled, readonly, multiple, selected, noresize, and defer.

Rationale: The compact form of these attributes is not well formed XML, and therefore invalid.

C.11 Document Object Model and XHTML → A.11. Document Object Model and XHTML

The Document Object Model level 1 Recommendation [DOM] defines document object model interfaces for XML and HTML 4. The HTML 4 document object model specifies that HTML element and attribute names are returned in upper-case. The XML document object model specifies that element and attribute names are returned in the case they are specified. In XHTML 1.0, elements and attributes are specified in lower-case. This apparent difference can be addressed in two ways:

  1. Applications that access XHTML documents served as Internet media type text/html via the DOM can use the HTML DOM, and can rely upon element and attribute names being returned in upper-case from those interfaces.
  2. Applications that access XHTML documents served as Internet media types text/xml or application/xml can also use the XML DOM. Elements and attributes will be returned in lower-case. Also, some XHTML elements may or may not appear in the object tree because they are optional in the content model (e.g. the tbody element within table). This occurs because in HTML 4 some elements were permitted to be minimized such that their start and end tags are both omitted (an SGML feature). This is not possible in XML. Rather than require document authors to insert extraneous elements, XHTML has made the elements optional. Applications need to adapt to this accordingly.

DO rely upon the HTML 4 DOM as defined in The Document Object Model level 1 Recommendation [DOM] for scripting. This means, in particular, that the names of elements and attributes will be returned (from functions that return such things) in upper case.

Rationale: Using the HTML DOM will result in maximum portability of scripts, since the HTML DOM is supported in both HTML and XHTML documents in modern user agents.

C.12 Using Ampersands in Attribute Values → A.12. Using Ampersands

When an attribute value contains an ampersand, it must be expressed as a character entity reference (e.g. "&amp;"). For example, when the href attribute of the a element refers to a CGI script that takes parameters, it must be expressed as http://my.site.dom/cgi-bin/myscript.pl?class=guest&amp;name=user rather than as http://my.site.dom/cgi-bin/myscript.pl?class=guest&name=user.

DO ensure that when content or attribute values contain the reserved character & it is used in its escaped form &amp;.

Rationale: If ampersands are not encoded, the characters after them up to the next semi-colon can be interpreted as the name of a entity by the user agent.

@@@@add example@@@@

C.13 Cascading Style Sheets (CSS) and XHTML → A.13. Cascading Style Sheets (CSS) and XHTML

The Cascading Style Sheets level 2 Recommendation [CSS2] defines style properties which are applied to the parse tree of the HTML or XML document. Differences in parsing will produce different visual or aural results, depending on the selectors used. The following hints will reduce this effect for documents which are served without modification as both media types:

  1. CSS style sheets for XHTML should use lower case element and attribute names.
  2. In tables, the tbody element will be inferred by the parser of an HTML user agent, but not by the parser of an XML user agent. Therefore you should always explicitly add a tbody element if it is referred to in a CSS selector.
  3. Within the XHTML name space, user agents are expected to recognize the "id" attribute as an attribute of type ID. Therefore, style sheets should be able to continue using the shorthand "#" selector syntax even if the user agent does not read the DTD.
  4. Within the XHTML name space, user agents are expected to recognize the "class" attribute. Therefore, style sheets should be able to continue using the shorthand "." selector syntax.
  5. CSS defines different conformance rules for HTML and XML documents; be aware that the HTML rules apply to XHTML documents delivered as HTML and the XML rules apply to XHTML documents delivered as XML.

DO use lower case element and attribute names in style sheets. DO create rules that include inferred elements (e.g., the tbody element in a table).

Rationale: These simple rules will help increase the portability of CSS rules regardless of the media type the document is processed as.

@@@@add examples@@@@

→ A.14. Referencing Style Elements when serving as XML  

DO NOT use xml stylesheet declarations to identify style sheets.

DO use the style or link elements to define stylesheets.

Rationale: Since XML processing instructions may be rendered by some HTML user agents, using the standard XML stylesheet declaration mechanism may not work well. However, since XHTML user agents are required to process style and link elements and interpret stylesheets referenced from those elements, documents constructed to use them will work as expected.

→ A.15. Formfeed Character in HTML vs. XML  

Rationale: This character is recognized as white space in HTML 4, but is NOT considered white space in XML.

→ A.16. The Named Character Reference &apos;  

DO use &#39; to specify an escaped apostrophe. DO NOT use &apos;.

Rationale: The entity &apos; is not defined in HTML 4.

XHTML Media Types Second Edition が今後ドラフトになれば XML 宣言を省略する機会も増える ?

......さて、だいぶ話がそれましたが、冒頭で「 IE 6 がバグによってレンダリングモードが切り替わってしまうから、という理由だけで安易に XML 宣言を書かない、という対処をして良いものなのでしょうか」と自問していましたが、今後 XHTML Media Types Second Edition が Recommendation track に乗るようになっていけば、 XML 宣言の記述を行う・行わないの選択の理由が増えますね。

今回は自分の拙い解釈・邦訳で考えている部分があるので、「いや その解釈は おかしい」とか「それ邦訳じゃなくて超訳やん」といったご指摘をよろしくお願いします。

あと勘のいい方なら気が付いているかもしれませんが、今回の内容や昨日の DOCTYPE スイッチについてのまとめと一覧表 (HTML 5 や IE 8 Beta 2 のモードスイッチなどの情報も含んだ 2008 年版 ) は、以前実践 Web Standards Design に執筆した内容です。

リプライ

1 件のリプライが送られています。

2008-10-01T20:34+09:00 - No beer, No Name!

「XML 宣言を省略できる条件、というのは実は仕様には書かれてありません」とありますが、各XHTMLの仕様書の適合条件のセクションに書いてありますね。仕様ごとにutf-8/utf-16以外の文字エンコーディングをHTTPヘッダで示す方法が可能だったりいけなかったりしてややこしいのですが。

この記事に対するご意見やご質問、ご感想などありましたらこのフォームに簡潔に記入して下さい。 簡潔に記入できない場合や、関連記事にてご意見をお寄せいただく場合は、ご自身の weblog にて記事を書かれた上で あてにトラックバックとして送信してください。

記入フォーム

補足情報

著作、講演、制作実績など