aboutsummaryrefslogtreecommitdiff
path: root/files/es/web/api/document/evaluate/index.html
blob: 67bc5d432e0faceb676d6a9b9366d4ca29e06999 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
---
title: Document.evaluate()
slug: Web/API/Document/evaluate
translation_of: Web/API/Document/evaluate
---
<div>{{ ApiRef("DOM") }}</div>

<p>Retorna <code><a href="/en-US/docs/XPathResult" title="XPathResult">XPathResult</a></code> basado en una expresión <a href="/en-US/docs/XPath" title="XPath">XPath</a> y otros parametros dados .</p>

<h2 id="Syntax" name="Syntax">Sintaxis</h2>

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

<ul>
 <li><code>XpathExpression es una cadena que representa el XPath que se va a evaluar.</code></li>
 <li><code>contextNode</code> specifies the <em>context node</em> for the query (see the [<a class="external" href="http://www.w3.org/TR/xpath" rel="freelink">http://www.w3.org/TR/xpath</a> XPath specification). It's common to pass <code>document</code> as the context node.</li>
 <li><code>namespaceResolver</code> is a function that will be passed any namespace prefixes and should return a string representing the namespace URI associated with that prefix. It will be used to resolve prefixes within the XPath itself, so that they can be matched with the document. <code>null</code> is common for HTML documents or when no namespace prefixes are used.</li>
 <li><code>resultType</code> is an integer that corresponds to the type of result <code>XPathResult</code> to return. Use <a href="#Result_types">named constant properties</a>, such as <code>XPathResult.ANY_TYPE</code>, of the XPathResult constructor, which correspond to integers from 0 to 9.</li>
 <li><code>result</code> is an existing <code>XPathResult</code> to use for the results. <code>null</code> is the most common and will create a new <code>XPathResult</code></li>
</ul>

<h2 id="Example" name="Example">Ejemplo</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">Nota</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">Tipos de resultados</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">Especificaciones</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">Compatibilidad del navegador</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>XPath 1.0</td>
   <td>{{CompatChrome(1.0)}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop(1.8)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatOpera(9.0)}}</td>
   <td>{{CompatVersionUnknown}}<sup>[1]</sup></td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Edge</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>XPath 1.0</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] Implemented in WebKit 5.0 (531) or earlier.</p>

<h2 id="See_also" name="See_also">Ver también</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>