You're not going to find it in the lxml documentation because it's basic xpath semantics, `/` at the start of the path means the path is absolute and thus the search is anchored to the document root: https://www.w3.org/TR/xpath-10/#location-paths:~:text=An%20absolute%20location%20path

> An absolute location path consists of / optionally followed by a relative location path. A / by itself selects the root node of the document containing the context node. If it is followed by a relative location path, then the location path selects the set of nodes that would be selected by the relative location path relative to the root node of the document containing the context node.

To search within the context node (the node from which you're invoking `xpath`) you can:

- use `self::node()` or `.` (an abbreviation for the former) as the
  leading step
- or follow the descendant (or descendant-or-self) axis explicitly e.g.
  `descendant::table` should have the same effect as `.//table`

On 16/02/2026 16:15, lord of edges via lxml - The Python XML Toolkit wrote:
Will start by saying I am sure this is documented somehow and I probably just 
don't know the right word to find that documentation.

I'm trying to query a table from a document, after which I will query details 
from that table. The issue I have is that the second xpath query is pulling 
results from the entire document.

As an example, I have:

```
from lxml import html

exampledocument="""
<head>
example
</head>
<body>
<table><tbody>
<tr><th>exampleCellNotToFind</th>
</tbody></table>
<table id="exampleTableToFind"><tbody>
<tr><th>exampleCellToFind</th>
</tbody></table>
</body>
"""

example=html.fromstring(exampledocument)

xpath1=example.xpath('//table[@id="exampleTableToFind"]', smart_strings=False)
xpath2=xpath1[0].xpath('//table', smart_strings=False)
xpath3=html.fromstring(html.tostring(xpath1[0])).xpath('//table', 
smart_strings=False)
```

In this case, xpath2 includes both tables (xpath1 only includes 1 table), and I don't 
understand why. From reading "XPath return values" section of 
https://lxml.de/xpathxslt.html#xpath I thought smart_strings=True would the reason, but 
setting it to false didn't seem to change the output as far as I can tell.

I do have a workable solution in this html.fromstring(html.tostring()), but 
thought I should ask here to try to understand how these element type variables 
are working.

Thanks, for any help.
_______________________________________________
lxml - The Python XML Toolkit mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/lxml.python.org
Member address: [email protected]

_______________________________________________
lxml - The Python XML Toolkit mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/lxml.python.org
Member address: [email protected]

Reply via email to