DOM Level 3 XPath

DOM XPath

[43] DOM XPath は、 XPath 1.0 を使ってDOM木上の節点を取得する API です。

[44] 現行のすべての主要 Webブラウザーに実装されています。かつてはよく用いられていましたが、 異様に複雑な API のため、 Selectors API が実装されてからはほとんど使うこともなくなりました。

代替

[46] Selectors API と通常の DOM API でほとんど代用できます。

仕様書

[42] DOM3 XPath が10年以上メンテナンスされておらず、事実上標準不在状態となっています。

歴史

MSXML XPath

[15] IRC logs: freenode / #whatwg / 20101008 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20101008#l-419

DOM3 XPath

[1] Introduction to using XPath in JavaScript - MDC http://developer.mozilla.org/en/docs/Introduction_to_using_XPath_in_JavaScript (名無しさん)

[2] Latest topics > getElementsByなんちゃら の代わりにXPathを使う - outsider reflex (Piro(SHIMODA Hiroshi) 著, 2007-09-09 11:44:24 +09:00 版) http://piro.sakura.ne.jp/latest/blosxom/mozilla/xul/2007-09-09_xpath.htm (名無しさん)

[3] SourceForge.net: XPath over HTML for MSIE (2007-10-06 15:19:21 +09:00 版) http://sourceforge.net/projects/html-xpath/ (名無しさん)

[4] Safari 3 の XPath のバグ: Days on the Moon (2008-05-11 15:36:16 +09:00 版) http://nanto.asablo.jp/blog/2007/11/10/1901281 (名無しさん)

[5] 選択範囲のリンクを取得する: Days on the Moon (2008-10-19 11:34:21 +09:00 版) http://nanto.asablo.jp/blog/2008/10/18/3829312 (名無しさん)

[6] XPath は実用に耐えうるかの考察と xpath.js - IT戦記 (2008-10-31 22:50:48 +09:00 版) http://d.hatena.ne.jp/amachang/20070828/1188306956 (名無しさん)

[7] HTML 用の XPath 機能テストを書いた - IT戦記 ( 版) http://d.hatena.ne.jp/amachang/20071009/1191903856

[8] JavaScript-XPath をリリースしました!さあ、あなたも XPath を使おう!(解説付き) - IT戦記 ( 版) http://d.hatena.ne.jp/amachang/20071112/1194856493

[9] JavaScript-XPath – CodeRepos::Share – Trac ( 版) http://coderepos.org/share/wiki/JavaScript-XPath

[10] はてなブックマークのコンテンツの JavaScript を高速化する - IT戦記 ( 版) http://d.hatena.ne.jp/amachang/20081126/1227700830

[11] XPath, $X function, NSResolver < 16 < March < 2006 < nulog, NULL::something : out of the headphone ( 版) http://lowreal.net/logs/2006/03/16/1

[12] Kanasan.JS Greasemonkey チュートリアル読書会: Days on the Moon ( 版) http://nanto.asablo.jp/blog/2009/03/22/4197758

document.evaluate メソッドの返り値である XPathResult オブジェクトを、別の evaluate メソッドの呼び出し時に第5引数として渡すと、その XPathResult オブジェクトが再利用されます。しかし、「再利用される」とは具体的にどうなることかという説明は DOM 3 XPath 仕様中になく、そもそも "may be reused" (強調は筆者による) なので確実に再利用されるという保証もありません。Firefox (Gecko) と Opera では第 5 引数に渡したオブジェクトが返り値となりますが、Safari (WebKit) では常に新しいオブジェクトを生成して返すようです。ちなみに Firefox では第 5 引数を指定すると意図しない結果になることがあります。

var result = document.evaluate("'foo'", document, null,
                               XPathResult.STRING_TYPE, null);
document.evaluate("'bar'", document, null,
                  XPathResult.STRING_TYPE, result);
result.stringValue;
// Firefox 3.6a1pre => "foobar" ("bar" を期待していたのに!)
// Opera 9.64       => "bar"
// Safari 3.1.2     => "foo"

XPathResult には結果の型として順序付きのもの (ORDERED_NODE_ITERATOR_TYPE、ORDERED_NODE_SNAPSHOT_TYPE、FIRST_ORDERED_NODE_TYPE) と順序なしのもの (UNORDERED_NODE_ITERATOR_TYPE、UNORDERED_NODE_SNAPSHOT_TYPE、ANY_UNORDERED_NODE_TYPE) があります。ソースコードを見る限り、WebKit では順序付きかどうかで処理を分けていますが、Gecko では順序なしでも順序付きと同等に扱っているようです。

[16] DOM XPath - WHATWG Wiki ( ( 版)) http://wiki.whatwg.org/wiki/DOM_XPath

[17] Document Object Model (DOM) Level 3 XPath Specification ( ( 版)) http://www.w3.org/TR/DOM-Level-3-XPath/

[18] Document Object Model (DOM) Level 3 XPath Specification ( ( 版)) http://www.w3.org/TR/2004/NOTE-DOM-Level-3-XPath-20040226/DOM3-XPath.html

[19] Document Object Model (DOM) Requirements ( ( 版)) http://www.w3.org/TR/2004/NOTE-DOM-Requirements-20040226/#dom-level-3-requirements-level3-xpath

[23] Firefoxdocument.evaluate の引数を省略できません。 Chrome では最初の2つ以外は省略できます。

[35] Firefox は最後の引数がオブジェクトでなくプリミティブ値だと例外を投げます。 Chrome では例外になりません。

[30] Introduction to DOM XPath - DOM ECMAScripting ( ( 版)) http://domes.lingua.heliohost.org/dom/intro-xpath1.html

[31] Firefox でも Chrome でも文脈節点DocumentFragment にすると NotSupportedError 例外を投げます。

[36] Chrome では構文解析時でない評価時のエラーは SyntaxError にします。 FirefoxDOMException ではなさそうな謎のエラーにします。

XPath と HTML

[13] (X)HTML5 Tracking ( 版) http://html5.org/tools/web-apps-tracker?from=4006&to=4007

[14] Bug 7059 – Forking XPath ( 版) http://www.w3.org/Bugs/Public/show_bug.cgi?id=7059

[22] FirefoxChrome も、既定名前空間については NSResolver を呼び出しません (仕様通りの動作)。 HTML文書では既定名前空間名前テストHTML名前空間要素一致します。 Firefox では更に null名前空間要素とも一致します。 XML文書では FirefoxChromenull名前空間要素とだけ一致します。どちらの場合もその他の名前空間とは一致しません。

構文解析

[26] XPathNSResolver の項も参照。

[25] Firefox関数名名前空間接頭辞解決した上で、未対応の関数があれば構文解析エラーとします。 Chrome関数名前空間接頭辞解決せず、未対応の関数があれば構文解析エラーとします。

[28] Firefox関数引数の個数チェックを評価時に行っているようですが、 Chrome構文解析時に行っているようです。

[29] FirefoxChrome引数の型チェックは構文解析時ではなく評価時に行っているようです。 例えば count(12) は構文解析エラーになりませんが評価時に例外が投げられます。 | 演算子も同様です。

[27] Firefox変数が入ったを構文解析できますが、評価時に NS_ERROR_ILLEGAL_VALUE 例外が発生します。なので createExpression は成功しますが document.evaluate例外を投げます。 Chrome変数名前空間接頭辞があると SyntaxError になります。名前空間接頭辞がなければ評価も成功します (空文字列になるようです)。

[32] Document Object Model (DOM) Requirements ( ( 版)) http://www.w3.org/TR/2004/NOTE-DOM-Requirements-20040226/#dom-level-3-requirements-level3-xpath

[33] Introduction to using XPath in JavaScript | MDN ( ( 版)) https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript

[34] XPath 1.0 in JavaScript • Cameron McCormack's blog ( ( 版)) http://mcc.id.au/xpathjs

[37] DOM Level 3 XPath Test Suite ( ( 版)) http://www.w3.org/2003/02/19-dom-xpath-implementation.html

[38] Firefoxevaluate メソッド文脈オブジェクトたる Document文脈節点として指定した節点文書が異なると WRONG_DOCUMENT_ERR を投げるようです (DOM3 XPath 仕様に従った動作)。 Chrome は例外を投げずに処理を続けます。

[39] 376740 – XPath expressions in HTML must be lowercase ( ( 版)) https://bugzilla.mozilla.org/show_bug.cgi?id=376740

[40] >>39 ChromeHTML要素の大文字と小文字を区別しないようですが Firefox は区別します。

[45] IRC logs: freenode / #whatwg / 20150903 ( 版) http://krijnhoetmer.nl/irc-logs/whatwg/20150903

# [04:43] <Domenic> https://www.chromestatus.com/metrics/feature/timeline/popularity/297 https://www.chromestatus.com/metrics/feature/timeline/popularity/295 https://www.chromestatus.com/metrics/feature/timeline/popularity/296 usage stats... could in theory remove createExpression?

# [04:44] <Domenic> Which would be nice because then we could kill XPathExpression entirely

[47] find: Correct implementation of "xpath" strategy (jugglinmike著, ) https://github.com/w3c/webdriver/commit/bbcbfbad1568296d84e75bc3edf5a7db311c58b1

[48] domxpath/interfaces.html has no spec to back it up · Issue #7562 · w3c/web-platform-tests () https://github.com/w3c/web-platform-tests/issues/7562

[49] Make the XPath IDL tests tentative by foolip · Pull Request #7639 · w3c/web-platform-tests () https://github.com/w3c/web-platform-tests/pull/7639

[50] Add the DOM XPath interfaces from the WHATWG wiki (annevk著, ) https://github.com/whatwg/dom/commit/dea5d92156f144faf57d1a5e54a17227139ca3c5

[51] Copy the DOM XPath interfaces from the WHATWG wiki by foolip · Pull Request #763 · whatwg/dom () https://github.com/whatwg/dom/pull/763

[52] Consider specifying document.evaluate and document.createNSResolver · Issue #67 · whatwg/dom () https://github.com/whatwg/dom/issues/67