$x

From FirebugWiki

(Difference between revisions)
Jump to: navigation, search
(Added description of syntax and linked back to Command Line API)
(Added feature descriptions of issue 18)
Line 5: Line 5:
== Syntax ==
== Syntax ==
<source lang="javascript">
<source lang="javascript">
-
$x(xpath)
+
$x(xpath [, contextNode [, resultType]])
</source>
</source>
Line 11: Line 11:
=== 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("//p")
+
$x("//h1")
</source>
</source>
-
This returns all <code>&lt;p&gt;</code>s of a page.
+
This returns an array of all <code>&lt;h1&gt;</code>s of a page.
<source lang="javascript">
<source lang="javascript">
Line 24: 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>&lt;h1&gt;</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 30: Line 57:
</source>
</source>
-
This returns all text nodes within every first <code>&lt;b&gt;</code> of all <code>&lt;p&gt;</code>s of a page.
+
This returns an array of all text nodes within every first <code>&lt;b&gt;</code> of all <code>&lt;p&gt;</code>s of a page.
 +
 
 +
<source lang="javascript">
 +
$x(".//button", $$("form")[1])
 +
</source>
 +
 
 +
This returns an array of all <code>&lt;buttons&gt;</code> within the second <code>&lt;form&gt;</code> of a page. (requires Firebug 1.11 or higher)
 +
 
 +
<source lang="javascript">
 +
$x("//p", document, "node")
 +
</source>
 +
 
 +
This returns the first <code>&lt;p&gt;</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]]
* [[Command Line API]]

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

Syntax

$x(xpath [, contextNode [, resultType]])

Parameters

xpath

XPath expression used to match the elements to return. This follows the syntax of document.evaluate(). (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)

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

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)

See also

Personal tools