Document.evaluate()

XPath ๅผใ‚„ใใฎไป–ไธŽใˆใ‚‰ใ‚ŒใŸใƒ‘ใƒฉใƒกใƒผใ‚ฟใซๅŸบใฅใ„ใฆ XPathResult ใ‚’่ฟ”ใ—ใพใ™ใ€‚
 

ๆง‹ๆ–‡

var xpathResult = document.evaluate(
 xpathExpression,
 contextNode,
 namespaceResolver,
 resultType,
 result
);
  • xpathExpression ใฏ่ฉ•ไพกใ•ใ‚Œใ‚‹ Xpath ใ‚’็คบใ™ๆ–‡ๅญ—ๅˆ—ใงใ™ใ€‚
  • contextNode ใฏใ‚ฏใ‚จใƒชใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใƒŽใƒผใƒ‰ใ‚’ๆŒ‡ๅฎšใ—ใพใ™(XPath ใฎไป•ๆง˜ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„)ใ€€ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใƒŽใƒผใƒ‰ใจใ—ใฆ document ใ‚’ๆธกใ™ใฎใŒไธ€่ˆฌ็š„ใงใ™ใ€‚
  • namespaceResolver ใฏใ€ไปปๆ„ใฎๅๅ‰็ฉบ้–“ๆŽฅ้ ญ่พžใ‚’ๅ—ใ‘ๅ–ใ‚Šใ€ใใฎๆŽฅ้ ญ่พžใซ้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚ŒใŸๅๅ‰็ฉบ้–“ URI ใ‚’ๆ–‡ๅญ—ๅˆ—ใจใ—ใฆ่ฟ”ใ™้–ขๆ•ฐใงใ™ใ€‚ๆŽฅ้ ญ่พžใŒๆ–‡ๆ›ธใฎไธญใงใƒžใƒƒใƒใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ€XPath ่‡ช่บซใฎไธญใงๆŽฅ้ ญ่พžใ‚’่งฃๆฑบใ™ใ‚‹ใŸใ‚ใซไฝฟใ‚ใ‚Œใพใ™ใ€‚HTML ๆ–‡ๆ›ธๅ‘ใ‘ใ€ใพใŸใฏๅๅ‰็ฉบ้–“ๆŽฅ้ ญ่พžใ‚’ไฝฟใ‚ใชใ„ใจใใฏ null ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใฎใŒไธ€่ˆฌ็š„ใงใ™ใ€‚
  • resultType ใฏ่ฟ”ใ‚Šๅ€คใงใ‚ใ‚‹ XPathResult ใฎใ‚ฟใ‚คใƒ—ใซๅฏพๅฟœใ™ใ‚‹ๆ•ดๆ•ฐใงใ™ใ€‚XPathResult ใ‚ณใƒณใ‚นใƒˆใƒฉใ‚ฏใ‚ฟใฎๅๅ‰ไป˜ใๅฎšๆ•ฐใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃ (ไพ‹ใˆใฐ XPathResult.ANY_TYPE) ใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ใ“ใ‚Œใ‚‰ใฏ0ใ‹ใ‚‰9ใพใงใฎๆ•ดๆ•ฐใซๅฏพๅฟœใ—ใฆใ„ใพใ™ใ€‚
  • result ใฏ็ตๆžœใจใ—ใฆไฝฟใ‚ใ‚Œใ‚‹ๆ—ขๅญ˜ใฎ XPathResult ใงใ™ใ€‚null ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใฎใŒๆœ€ใ‚‚ไธ€่ˆฌ็š„ใงใ™ใ€‚null ใฏๆ–ฐใ—ใ„ XPathResultใ€€ใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚

ไพ‹

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

Note, in the above example, a more verbose XPath is preferred over common shortcuts such as //h2. 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 every node from the root and all subnodes looking for possible matches.

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:

document.evaluate(".//h2", document.body, null, XPathResult.ANY_TYPE, null);

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

See Introduction to using XPath in JavaScript for more information.

ๆณจ้‡ˆ

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

ๆˆปใ‚Šๅ€คใฎๅˆ†้กž

(Merge with Template:XPathResultConstants?

These are supported values for the resultType parameter of the evaluate method:

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.

ไป•ๆง˜

Specification Status Comment
Document Object Model (DOM) Level 3 XPath Specification
Document.evaluate ใฎๅฎš็พฉ
ๅ‹งๅ‘Š Initial specification

ใƒ–ใƒฉใ‚ฆใ‚ถไบ’ๆ›ๆ€ง

BCD tables only load in the browser

้–ข้€ฃ้ …็›ฎ