User:Marti/Greasemonkey Manual Addendum/XPathResult

From GreaseSpot

< User:Marti(Redirected from XPathResult)
Jump to: navigation, search

Contents

[edit] Description

XPathResult is added so that document.evaluate() works. Referential link to source code where this is part of the API is currently at #L253. Currently MDC does not have full reference and this article is key to interpreting not only the constants to values mapping but also why one would want to use the different types. When MDC is fully documented this page may be removed from this personal workspace.

[edit] Syntax

document.evaluate( xpathExpression, contextNode, namespaceResolver, XPathResult.resultType, XPathResult );

Value: Object
Returns: XPathResult
Compatibility: Greasemonkey 0.6+
XPathResult Object
Properties Methods resultType Constants
booleanValue iterateNext ANY_TYPE
invalidIteratorState snapshotItem NUMBER_TYPE
numberValue STRING_TYPE
resultType BOOLEAN_TYPE
singleNodeValue UNORDERED_NODE_ITERATOR_TYPE
snapshotLength ORDERED_NODE_ITERATOR_TYPE
stringValue UNORDERED_NODE_SNAPSHOT_TYPE
ORDERED_NODE_SNAPSHOT_TYPE
ANY_UNORDERED_NODE_TYPE
FIRST_ORDERED_NODE_TYPE
  • All properties, methods and constants are optional

[edit] Properties


[edit] booleanValue

Value: Boolean (readonly boolean)
Usage: alert( "Found a boolean with a value of " + XPathResult.booleanValue );
  • This is typically used if the XPathResult.BOOLEAN_TYPE resultType is used in document.evaluate to request a single boolean based off the xpathExpression.


[edit] invalidIteratorState

Value: Boolean (readonly boolean)
Usage: if (XPathResult.invalidIteratorState) { /* some code */ }
  • Use this to test if the DOM has been modified since last iteration (e.g. the last document.evaluate). If true then the current iteration in XPathResult may not include these recent changes.


[edit] numberValue

Value: Number (readonly float)
Usage: alert( "Found a number with a value of " + XPathResult.numberValue );
  • This is typically used if the XPathResult.NUMBER_TYPE resultType is used in document.evaluate to request a single number based off the xpathExpression.


[edit] resultType

Value: Number (readonly short)
Usage: if (XPathResult.resultType == XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE) { /* some code * }


[edit] singleNodeValue

Value: Object (readonly Node)
Usage: alert( XPathResult.singleNodeValue.textContent );


[edit] snapshotLength

Value: Number (readonly int)
Usage: if (XPathResult.snapshotLength > 0) { /* some code */ }


[edit] stringValue

Value: String (readonly String)
Usage: alert( "Found a string with a value of " + XPathResult.stringValue );
  • This is typically used if the XPathResult.STRING_TYPE resultType is used in document.evaluate to request a single string based off the xpathExpression.


[edit] Methods


[edit] iterateNext

Usage: var thisNode = XPathResult.iterateNext();
Returns: Object (Node)


[edit] snapshotItem

Usage: var thisNode = XPathResult.snapshotItem( index );
Returns: Object (Node)


[edit] resultType Constants


[edit] ANY_TYPE

Value: Number (readonly short)
Constant: 0
Usage: document.evaluate( xpathExpression, contextNode, namespaceResolver, XPathResult.ANY_TYPE, XPathResult);


[edit] NUMBER_TYPE

Value: Number (readonly short)
Constant: 1
Usage: document.evaluate( xpathExpression, contextNode, namespaceResolver, XPathResult.NUMBER_TYPE, XPathResult);


[edit] STRING_TYPE

Value: Number (readonly short)
Constant: 2
Usage: document.evaluate( xpathExpression, contextNode, namespaceResolver, XPathResult.STRING_TYPE, XPathResult);


[edit] BOOLEAN_TYPE

Value: Number (readonly short)
Constant: 3
Usage: document.evaluate( xpathExpression, contextNode, namespaceResolver, XPathResult.BOOLEAN_TYPE, XPathResult);


[edit] UNORDERED_NODE_ITERATOR_TYPE

Value: Number (readonly short)
Constant: 4
Usage: document.evaluate( xpathExpression, contextNode, namespaceResolver, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, XPathResult);


[edit] ORDERED_NODE_ITERATOR_TYPE

Value: Number (readonly short)
Constant: 5
Usage: document.evaluate( xpathExpression, contextNode, namespaceResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, XPathResult);


[edit] UNORDERED_NODE_SNAPSHOT_TYPE

Value: Number (readonly short)
Constant: 6
Usage: document.evaluate( xpathExpression, contextNode, namespaceResolver, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, XPathResult);


[edit] ORDERED_NODE_SNAPSHOT_TYPE

Value: Number (readonly short)
Constant: 7
Usage: document.evaluate( xpathExpression, contextNode, namespaceResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, XPathResult);


[edit] ANY_UNORDERED_NODE_TYPE

Value: Number (readonly short)
Constant: 8
Usage: document.evaluate( xpathExpression, contextNode, namespaceResolver, XPathResult.ANY_UNORDERED_NODE_TYPE, XPathResult);


[edit] FIRST_ORDERED_NODE_TYPE

Value: Number (readonly short)
Constant: 9
Usage: document.evaluate( xpathExpression, contextNode, namespaceResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, XPathResult);


[edit] Examples

// Log all snapshot second level headers (h2) tag element text nodes to the error console in reverse order for speed.

var xpr = document.evaluate("//h2", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

for (var i = xpr.snapshotLength - 1; i >= 0; --i)
  GM_log( xpr.snapshotItem(i).textContent );
/* Log all live second level headers (h2) tag element text nodes to the error console - Alternate method */

var xpr;

document.evaluate("//h2", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, xpr);

var thisNode;
while ((thisNode = xpr.iterateNext()))
  GM_log( thisNode.textContent );

[edit] See Also

[edit] Notes

Personal tools