XML宣言 (XMLDecl) は処理命令 (PI) ではない
XML宣言が PROCESSING_INSTRUCTION_NODE だと思っていて、以下のXHTML文書を書いていました。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja"> <head> <!-- /** * サーバは次のHTTPヘッダを出力しています。 * Content-Type: application/xhtml+xml; charset=UTF-8 */ --> <title>XHTML5</title> </head> <body> <script type="application/javascript"><![CDATA[ console.log(document.firstChild.nodeType === Node.DOCUMENT_TYPE_NODE); // true console.log(document.evaluate('child::processing-instruction()', document, null, 7, null).snapshotLength); // 0 console.log(document.createProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"').nodeType === Node.PROCESSING_INSTRUCTION_NODE); // true ]]></script> </body> </html>
しかし、XML宣言は PROCESSING_INSTRUCTION_NODE ではないので document からノードとして認識されません。
XML宣言の情報は Interface Document (DOM L3 Core) のプロパティとして定義されています。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja"> <head> <title>XHTML5</title> </head> <body> <script type="application/javascript"><![CDATA[ console.log(document.xmlVersion); // "1.0" console.log(document.xmlEncoding); // "UTF-8" console.log(document.xmlStandalone); // true ]]></script> </body> </html>
think49 think49
いい方法が見つからないなあ。/ JavaScriptでのXML解析について質問です。 JavaScriptでXMLファイルを読み込んで... - Yahoo!知恵袋 http://goo.gl/WLTcf #XML #DOM #JavaScript
think49 think49
XPath で PROCESSING_INSTRUCTION_NODE を得られると思ったら snapshotLength が 0 になってしまった。XPath式が間違ってるのかな。
http://goo.gl/zi5w9
http://goo.gl/zi5w9
think49 think49
document.firstChild が PROCESSING_INSTRUCTION_NODE ではなく、DOCUMENT_TYPE_NODE になる。
nanto_vi TOYAMA Nao
think49 think49
@nanto_vi ありがとうございます。2.6 Processing Instructions http://goo.gl/FG5SG も合わせて読んで理解できました。