Hi,

paths starting with / always start from the document root. They are not
relative to the current context element.

What you probably want is ".//table" where the dot marks the current
element as a search start.

lord of edges via lxml - The Python XML Toolkit wrote (at 2026-02-16 15:15 
-0000):
> 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