DOM:document.evaluate
z Mozilla Developer Center, polskiego centrum programistów Mozilli.
UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...
Spis treści |
[edytuj] Podsumowanie
Returns an XPathResult based on an XPath expression and other given parameters.
[edytuj] Składnia
var xpathResult = document.evaluate( xpathExpression, contextNode, namespaceResolver, resultType, result);
-
xpathExpressionis a string representing the XPath to be evaluated. -
contextNodeis the node inside which the search will take place.documentis most common. -
namespaceResolveris 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.nullis common for HTML documents or when no namespace prefixes are used. -
resultTypeis an integer that corresponds to the type of resultXPathResultto return. Use named constant properties, such asXPathResult.ANY_TYPE, of the XPathResult constructor, which correspond to integers from 0 to 9. -
resultis an existingXPathResultto use for the results.nullis most common and will create a newXPathResult
[edytuj] Przykład
From the Mozilla XPath Tutorial:
var headings = document.evaluate("//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
[edytuj] Notatki
XPath expressions can be evaluated on HTML and XML documents.
[edytuj] Result Types
| Result Type | Value | Description |
| ANY_TYPE | 0 | Whatever type naturally results from the given expression. |
| NUMBER_TYPE | 1 | A result set containing a single number. Useful, for example, in an XPath expression using the count() function.
|
| STRING_TYPE | 2 | A result set containing a single string. |
| BOOLEAN_TYPE | 3 | A result set containing a single boolean value. Useful, for example, an an XPath expression using the not() function.
|
| UNORDERED_NODE_ITERATOR_TYPE | 4 | 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. |
| ORDERED_NODE_ITERATOR_TYPE | 5 | 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. |
| UNORDERED_NODE_SNAPSHOT_TYPE | 6 | 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. |
| ORDERED_NODE_SNAPSHOT_TYPE | 7 | 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. |
| ANY_UNORDERED_NODE_TYPE | 8 | 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. |
| FIRST_ORDERED_NODE_TYPE | 9 | A result set containing the first node in the document that matches the expression. |
Results of NODE_ITERATOR 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.
Results of NODE_SNAPSHOT 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.