Free tools Windows power users keep installed
One-click scans. No signup required.
XPath positions start at 1, and where you put the position predicate determines what gets counted. Use //catalog/item[3] for the third item child under each matching catalog; use (//catalog/item)[3] for the third item in the complete result sequence. The parentheses change the scope from each step’s local candidates to the overall path result.
Choose the position scope you mean
XPath does not use zero-based indexes: the first item is position 1, the second is position 2, and so on. The key question is not just which number to use, but which sequence that number applies to. A predicate attached directly to a path step selects from that step’s candidates in its context. Parenthesizing a complete path first lets a predicate filter the resulting sequence as a whole.
| What you want | XPath | What the position counts |
|---|---|---|
Third item child under each matching catalog |
//catalog/item[3] |
The item children in each relevant catalog context |
First item child under each matching catalog |
//catalog/item[1] |
The item children in each relevant catalog context |
Third item in the complete path result |
(//catalog/item)[3] |
The items returned by the parenthesized path |
Third item at the step, written explicitly |
//catalog/item[position() = 3] |
The item candidates in each relevant step context |
For example, if two catalogs each contain three items, //catalog/item[3] can return two nodes: the third child from each catalog. By contrast, (//catalog/item)[3] filters the full path result to one node, assuming at least three items occur in that result.
How positional predicates work
A predicate is the bracketed test on a path step. In //catalog/item[3], the numeric predicate is equivalent to [position() = 3]: it keeps the candidate whose context position is 3. The context is determined by the step and path leading to it; XPath does not simply number all matching element names across the document unless you first form and filter that full result sequence.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minute#1 Best Overall
The W3C XPath 3.1 Recommendation, published 21 March 2017, states that the first item in a sequence has position 1. This one-based rule also applies to the ordinary numeric positional syntax used in XPath 1.0 and 2.0. MDN’s position() reference, last modified 10 June 2025, likewise describes the first node as position 1 and notes that the surrounding path determines the context.
Direct child positions
With an XML structure such as <catalog><item/><item/><item/></catalog>, /catalog/item[2] selects the second item child of the document’s catalog. If the path begins with //catalog, there may be several matching catalog elements, and the item[2] step is evaluated in the context of each matching catalog.
Whole-result positions
Use parentheses when you mean “the nth node returned by this whole path,” rather than “the nth matching child in every step context.” For example, (//item)[1] filters the complete result of //item to its first item. This distinction is the usual reason //item[1] appears to return more than one node: it means first item in each relevant parent context, not the first document-wide match.
Rank #2
- Used Book in Good Condition
Filter first or position first
Adjacent predicates are applied from left to right. As a result, moving a condition before or after a position predicate can change the result.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →| Expression | Order of operations | Meaning |
|---|---|---|
//item[@type='x'][2] |
First keep matching items with type='x', then select position 2 |
Second qualifying item in each step context |
//item[2][@type='x'] |
First select position 2, then test its type |
The second item only if that item has type='x' |
If your intent is “the second item that meets this condition,” put the condition first. If you mean “the second item, provided it meets this condition,” put the position first. Keep parentheses in mind as a separate scope decision: (//item[@type='x'])[2] applies the position to the complete filtered result, rather than independently within each parent context.
Select the last or next-to-last match
When the target is defined relative to the end, XPath provides last(), which evaluates to the size of the current predicate context. Use it directly or in arithmetic:
//catalog/item[last()]selects the lastitemchild in each matching catalog context.//catalog/item[last() - 1]selects the second-to-lastitemchild in each matching catalog context.(//item)[last()]selects the last item in the complete parenthesized result.
The first and third examples differ in scope in the same way as their numeric-position counterparts. If a context has no matching items, a positional predicate cannot produce a matching item from it. A second-to-last expression likewise needs a context with at least two candidates to return the intended node.
Reverse axes and document order
Axis direction affects the context positions used by predicates. A reverse axis such as preceding::foo examines nodes preceding the context node in reverse document order. Therefore preceding::foo[1] selects the nearest preceding foo, not the earliest such element in document order.
Recommended Free Tools
Parentheses can change the sequence to which the predicate applies. In (preceding::foo)[1], the predicate filters the parenthesized sequence in document order, so the result can differ from preceding::foo[1]. XPath’s final node results are in document order, but that does not mean a reverse-axis predicate assigns its context positions in forward order. When an axis is involved, decide whether you mean the nearest match in that axis’s direction or a position in a separately formed result sequence.
Use XPath in the version your host supports
Positional predicates are available in XPath 1.0, 2.0, and 3.1. XPath 1.0 uses node-sets; XPath 2.0 and 3.1 define predicates over sequences while retaining the rule that a numeric predicate matches the context position. XPath 3.1 is a W3C Recommendation and compatible extension of XPath 3.0; its maps and arrays are not needed for ordinary element indexing.
The XPath version available to you depends on the application embedding XPath. A browser, scraper, XML editor, or other runtime may support a particular version or subset. The core positional forms in this guide are longstanding, but do not assume that a host supports every later XPath feature. Consult the documentation for the specific application before relying on version-specific syntax beyond basic predicates, position(), and last().
A practical method for writing and checking the expression
- Name the nodes. Write the path that identifies the relevant elements, such as
//catalog/item. - Decide the scope. If the position is relative to each parent or step context, add the predicate directly:
//catalog/item[3]. If it is relative to the complete result, group the path first:(//catalog/item)[3]. - Apply attribute or other filters in the intended order. Put a condition before the position when you want the nth candidate that passes it, as in
//item[@type='x'][2]. - Check whether the axis reverses direction. With a reverse axis, position 1 can mean the nearest preceding match. Do not infer the direction from the final display order.
- Evaluate against a known sample. Inspect the XML or application’s XPath result and compare the selected nodes with their parents and siblings. Include multiple matching parents in the sample if you need to distinguish local positions from the global result.
The expressions above are XPath expressions, not standalone shell commands: evaluating them requires an application or library that accepts XPath and a document to query. Since host support varies, use the host’s own XPath evaluation facility and verify its supported version rather than assuming a particular browser or runtime behavior.
Best Value
Common mistakes and fixes
- Using zero for the first result: XPath positions are one-based. Change a zero-based expectation of the first match to
[1]. - Getting multiple results from
//item[1]: The predicate is attached to theitemstep and selects the first item in each relevant context. Use(//item)[1]if the requirement is the first item in the complete result. - Finding the wrong item after an attribute filter: Predicate order matters. Use
//item[@type='x'][2]to choose the second item among those matching the attribute;//item[2][@type='x']instead checks whether the second item has that attribute. - Getting the wrong preceding node:
preceding::foo[1]uses reverse-axis context order and means the nearest preceding match. Use parentheses only if you intend to filter the parenthesized sequence in document order. - Seeing no result for a late position: The context may contain fewer candidates than the requested position, or an earlier predicate may have filtered out candidates. Check the candidate set before changing the number.
- An expression works in one application but not another: The host applications may expose different XPath versions or support. Confirm the host’s documented XPath version before using features beyond the basic positional pattern.
Or skip the browser setup
ScreenshotNeo is a website screenshot API, not an XPath evaluator: it will not return the node selected by an XPath expression. If your goal is to inspect a web page visually, you can request a screenshot without setting up a browser capture script. The URL-based call returns an image or PDF; see the ScreenshotNeo API documentation for request options and response details.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
Quick Recap
ScreenshotNeo accepts cookie or consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each of those steps can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and responses identify the page verdict and billing status in headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots. Learn about ScreenshotNeo, then sign up for 1,000 free screenshots a month with no card.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

