$x
From FirebugWiki
Sebastianz (Talk | contribs) (Separate Command Line API page for $x()) |
Sebastianz (Talk | contribs) (Added feature descriptions of issue 18) |
||
| (One intermediate revision not shown) | |||
| Line 2: | Line 2: | ||
Currently this command is limited to node arrays. See [http://code.google.com/p/fbug/issues/detail?id=18 issue 18] for supporting other [https://developer.mozilla.org/en/XPathResult results] to be printed. | Currently this command is limited to node arrays. See [http://code.google.com/p/fbug/issues/detail?id=18 issue 18] for supporting other [https://developer.mozilla.org/en/XPathResult results] to be printed. | ||
| + | |||
| + | == Syntax == | ||
| + | <source lang="javascript"> | ||
| + | $x(xpath [, contextNode [, resultType]]) | ||
| + | </source> | ||
== Parameters == | == Parameters == | ||
=== xpath === | === xpath === | ||
XPath expression used to match the elements to return. This follows the syntax of <code>document.evaluate()</code>. '''(required)''' | XPath expression used to match the elements to return. This follows the syntax of <code>document.evaluate()</code>. '''(required)''' | ||
| + | |||
| + | === contextNode === | ||
| + | Element used as root node for the XPath expression. (optional; added in Firebug 1.11) | ||
| + | |||
| + | === resultType === | ||
| + | Object type returned by the function. (optional; added in Firebug 1.11) | ||
| + | |||
| + | {| class="wikitable" style="width:100%; vertical-align:top;" | ||
| + | |- bgcolor=lightgrey | ||
| + | ! style="width:180px;" | Option || <code>document.evaluate()</code> equivalent || Description | ||
| + | |- | ||
| + | | <code>"number"</code> || <code>XPathResult.NUMBER_TYPE</code> || Numerical value | ||
| + | |- | ||
| + | | <code>"string"</code> || <code>XPathResult.STRING_TYPE</code> || String value | ||
| + | |- | ||
| + | | <code>"bool"</code> || <code>XPathResult.BOOLEAN_TYPE</code> || Boolean value | ||
| + | |- | ||
| + | | <code>"node"</code> || <code>XPathResult.FIRST_ORDERED_NODE_TYPE</code> || Single HTML element | ||
| + | |- | ||
| + | | <code>"nodes"</code> || <code>XPathResult.UNORDERED_NODE_ITERATOR_TYPE</code> || Array of HTML elements | ||
| + | |} | ||
== Examples == | == Examples == | ||
<source lang="javascript"> | <source lang="javascript"> | ||
| - | $x("// | + | $x("//h1") |
</source> | </source> | ||
| - | This returns all <code>< | + | This returns an array of all <code><h1></code>s of a page. |
<source lang="javascript"> | <source lang="javascript"> | ||
| Line 19: | Line 45: | ||
</source> | </source> | ||
| - | This returns all checkboxes of a page. | + | This returns an array of all checkboxes of a page. |
| + | |||
| + | <source lang="javascript"> | ||
| + | $x("count(//h1)>5") | ||
| + | </source> | ||
| + | |||
| + | This checks whether there are more than five <code><h1></code>s in the page and returns <code>true</code> or <code>false</code>. (requires Firebug 1.11 or higher) | ||
<source lang="javascript"> | <source lang="javascript"> | ||
| Line 25: | Line 57: | ||
</source> | </source> | ||
| - | This returns all text nodes within every first <code><b></code> of all <code><p></code>s of a page. | + | This returns an array of all text nodes within every first <code><b></code> of all <code><p></code>s of a page. |
| + | |||
| + | <source lang="javascript"> | ||
| + | $x(".//button", $$("form")[1]) | ||
| + | </source> | ||
| + | |||
| + | This returns an array of all <code><buttons></code> within the second <code><form></code> of a page. (requires Firebug 1.11 or higher) | ||
| + | |||
| + | <source lang="javascript"> | ||
| + | $x("//p", document, "node") | ||
| + | </source> | ||
| + | |||
| + | This returns the first <code><p></code> inside the page. (requires Firebug 1.11 or higher) | ||
== See also == | == See also == | ||
* [https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript Introduction to using XPath in JavaScript] | * [https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript Introduction to using XPath in JavaScript] | ||
| + | * [[Command Line API]] | ||
Latest revision as of 13:57, 18 August 2012
This command returns an array of HTML or XML elements matching the given XPath expression. This means it is actually a shortcut for document.evaluate().
Currently this command is limited to node arrays. See issue 18 for supporting other results to be printed.
Contents |
[edit] Syntax
$x(xpath [, contextNode [, resultType]])
[edit] Parameters
[edit] xpath
XPath expression used to match the elements to return. This follows the syntax of document.evaluate(). (required)
[edit] contextNode
Element used as root node for the XPath expression. (optional; added in Firebug 1.11)
[edit] resultType
Object type returned by the function. (optional; added in Firebug 1.11)
| Option | document.evaluate() equivalent | Description |
|---|---|---|
"number" | XPathResult.NUMBER_TYPE | Numerical value |
"string" | XPathResult.STRING_TYPE | String value |
"bool" | XPathResult.BOOLEAN_TYPE | Boolean value |
"node" | XPathResult.FIRST_ORDERED_NODE_TYPE | Single HTML element |
"nodes" | XPathResult.UNORDERED_NODE_ITERATOR_TYPE | Array of HTML elements |
[edit] Examples
$x("//h1")
This returns an array of all <h1>s of a page.
$x("//input[@type='checkbox']")
This returns an array of all checkboxes of a page.
$x("count(//h1)>5")
This checks whether there are more than five <h1>s in the page and returns true or false. (requires Firebug 1.11 or higher)
$x("//p/b[1]/text()")
This returns an array of all text nodes within every first <b> of all <p>s of a page.
$x(".//button", $$("form")[1])
This returns an array of all <buttons> within the second <form> of a page. (requires Firebug 1.11 or higher)
$x("//p", document, "node")
This returns the first <p> inside the page. (requires Firebug 1.11 or higher)