aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/document/evaluate/index.html
blob: 493d923a99feda9a49f66f2bb1c22a6b277721f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
---
title: Document.evaluate()
slug: Web/API/Document/evaluate
translation_of: Web/API/Document/evaluate
---
<div>{{ ApiRef("DOM") }}</div>

<div><a href="/en-US/docs/XPath" title="XPath">XPath</a> 式やその他与えられたパラメータに基づいて <code><a href="/en-US/docs/XPathResult" title="XPathResult">XPathResult</a></code> を返します。</div>

<div> </div>

<h2 id="Syntax" name="Syntax">構文</h2>

<pre class="syntaxbox">var xpathResult = document.evaluate(
 xpathExpression,
 contextNode,
 namespaceResolver,
 resultType,
 result
);</pre>

<ul>
 <li><code>xpathExpression</code> は評価される Xpath を示す文字列です。</li>
 <li><code>contextNode</code> はクエリの<em>コンテキストノード</em>を指定します(<a href="http://www.w3.org/TR/xpath" rel="freelink">XPath の仕様</a>を参照してください) コンテキストノードとして <code>document</code> を渡すのが一般的です。</li>
 <li><code>namespaceResolver</code> は、任意の名前空間接頭辞を受け取り、その接頭辞に関連付けられた名前空間 URI を文字列として返す関数です。接頭辞が文書の中でマッチされるように、XPath 自身の中で接頭辞を解決するために使われます。HTML 文書向け、または名前空間接頭辞を使わないときは <code>null</code> を指定するのが一般的です。</li>
 <li><code>resultType</code> は返り値である <code>XPathResult</code> のタイプに対応する整数です。XPathResult コンストラクタの<a href="#Result_types">名前付き定数プロパティ</a> (例えば <code>XPathResult.ANY_TYPE</code>) を使用してください。これらは0から9までの整数に対応しています。</li>
 <li><code>result</code> は結果として使われる既存の <code>XPathResult</code> です。<code>null</code> を指定するのが最も一般的です。<code>null</code> は新しい <code>XPathResult を生成します。</code></li>
</ul>

<h2 id="Example" name="Example"></h2>

<pre class="brush: js">var headings = document.evaluate("/html/body//h2", document, null, XPathResult.ANY_TYPE, null);
/* Search the document for all h2 elements.
 * The result will likely be an unordered node iterator. */
var thisHeading = headings.iterateNext();
var alertText = "Level 2 headings in this document are:\n";
while (thisHeading) {
  alertText += thisHeading.textContent + "\n";
  thisHeading = headings.iterateNext();
}
alert(alertText); // Alerts the text of all h2 elements
</pre>

<p>Note, in the above example, a more verbose XPath is preferred over common shortcuts such as <code>//h2</code>. Generally, more specific XPath selectors as in the above example usually gives a significant performance improvement, especially on very large documents. This is because the evaluation of the query spends does not waste time visiting unnecessary nodes. Using // is generally slow as it visits <em>every</em> node from the root and all subnodes looking for possible matches.</p>

<p>Further optimization can be achieved by careful use of the context parameter. For example, if you know the content you are looking for is somewhere inside the body tag, you can use this:</p>

<pre class="brush: js">document.evaluate(".//h2", document.body, null, XPathResult.ANY_TYPE, null);
</pre>

<p>Notice in the above <code>document.body</code> has been used as the context instead of <code>document</code> so the XPath starts from the body element. (In this example, the <code>"."</code> is important to indicate that the querying should start from the context node, document.body. If the "." was left out (leaving <code>//h2</code>) the query would start from the root node (<code>html</code>) which would be more wasteful.)</p>

<p>See <a href="/en-US/docs/Introduction_to_using_XPath_in_JavaScript" title="Introduction to using XPath in JavaScript">Introduction to using XPath in JavaScript</a> for more information.</p>

<h2 id="Notes" name="Notes">注釈</h2>

<ul>
 <li>XPath expressions can be evaluated on HTML and XML documents.</li>
 <li>While using document.evaluate() works in FF2, in FF3 one must use someXMLDoc.evaluate() if evaluating against something other than the current document.</li>
</ul>

<h2 id="Result_types" name="Result_types">戻り値の分類</h2>

<p>(Merge with <a href="/Template:XPathResultConstants" title="Template:XPathResultConstants">Template:XPathResultConstants</a>?</p>

<p>These are supported values for the <code>resultType</code> parameter of the <code>evaluate</code> method:</p>

<table class="standard-table">
 <tbody>
  <tr>
   <td class="header">Result Type</td>
   <td class="header">Value</td>
   <td class="header">Description</td>
  </tr>
  <tr>
   <td><code>ANY_TYPE</code></td>
   <td>0</td>
   <td>Whatever type naturally results from the given expression.</td>
  </tr>
  <tr>
   <td><code>NUMBER_TYPE</code></td>
   <td>1</td>
   <td>A result set containing a single number. Useful, for example, in an XPath expression using the <code>count()</code> function.</td>
  </tr>
  <tr>
   <td><code>STRING_TYPE</code></td>
   <td>2</td>
   <td>A result set containing a single string.</td>
  </tr>
  <tr>
   <td><code>BOOLEAN_TYPE</code></td>
   <td>3</td>
   <td>A result set containing a single boolean value. Useful, for example, an an XPath expression using the <code>not()</code> function.</td>
  </tr>
  <tr>
   <td><code>UNORDERED_NODE_ITERATOR_TYPE</code></td>
   <td>4</td>
   <td>A result set containing all the nodes matching the expression. The nodes in the result set are not necessarily in the same order they appear in the document.</td>
  </tr>
  <tr>
   <td><code>ORDERED_NODE_ITERATOR_TYPE</code></td>
   <td>5</td>
   <td>A result set containing all the nodes matching the expression. The nodes in the result set are in the same order they appear in the document.</td>
  </tr>
  <tr>
   <td><code>UNORDERED_NODE_SNAPSHOT_TYPE</code></td>
   <td>6</td>
   <td>A result set containing snapshots of all the nodes matching the expression. The nodes in the result set are not necessarily in the same order they appear in the document.</td>
  </tr>
  <tr>
   <td><code>ORDERED_NODE_SNAPSHOT_TYPE</code></td>
   <td>7</td>
   <td>A result set containing snapshots of all the nodes matching the expression. The nodes in the result set are in the same order they appear in the document.</td>
  </tr>
  <tr>
   <td><code>ANY_UNORDERED_NODE_TYPE</code></td>
   <td>8</td>
   <td>A result set containing any single node that matches the expression. The node is not necessarily the first node in the document that matches the expression.</td>
  </tr>
  <tr>
   <td><code>FIRST_ORDERED_NODE_TYPE</code></td>
   <td>9</td>
   <td>A result set containing the first node in the document that matches the expression.</td>
  </tr>
 </tbody>
</table>

<p>Results of <code>NODE_ITERATOR</code> types contain references to nodes in the document. Modifying a node will invalidate the iterator. After modifying a node, attempting to iterate through the results will result in an error.</p>

<p>Results of <code>NODE_SNAPSHOT</code> types are snapshots, which are essentially lists of matched nodes. You can make changes to the document by altering snapshot nodes. Modifying the document doesn't invalidate the snapshot; however, if the document is changed, the snapshot may not correspond to the current state of the document, since nodes may have moved, been changed, added, or removed.</p>

<h2 id="Specifications" name="Specifications">仕様</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("DOM3 XPath", "xpath.html#XPathEvaluator-evaluate", "Document.evaluate")}}</td>
   <td>{{Spec2("DOM3 XPath")}}</td>
   <td>Initial specification</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザ互換性</h2>

<p>{{Compat("api.Document.evaluate")}}</p>

<h2 id="See_also" name="See_also">関連項目</h2>

<ul>
 <li><a href="/en-US/docs/DOM/document.createExpression" title="DOM/document.createExpression">DOM:document.createExpression</a></li>
 <li><a href="/en-US/docs/Code_snippets/XPath" title="Code_snippets/XPath">XPath Code Snippets</a></li>
 <li><a href="http://codepen.io/johan/full/ckFgn">Check for browser support</a></li>
</ul>