tester3
tester3

Reputation: 423

Xpath not working in Perl but works in browser

I am trying to fetch the data from the table cell with this:

my $data = $tree->findvalue('(.//table[@class="tab openTab"]/tbody/tr/td/text())[1]');

This XPath (.//table[@class="tab openTab"]/tbody/tr/td/text())[1] works perfect when I test it in browser with $x('(.//table[@class="tab openTab"]/tbody/tr/td/text())[1]') but refuses to work in Perl (no data returned). The Perl code is fine because I fetch some other data with the same $tree from the same source.

How could it be?

Upvotes: 1

Views: 85

Answers (1)

ikegami
ikegami

Reputation: 386361

Technically, every table has a TBODY element, even if its start and end tag are both omitted. Your browser knows this, and creates a TBODY in its object tree.

XML::LibXML is just an XML parser. Unlike SGML on which HTML is based, XML has no concept of implied elements. XML::LibXML has support for HTML syntax, but whether parsing XML or HTML, the document the parser returns is a representation of what's actually in the file. No TBODY element is added if none is found in the file.

So table/tbody/tr/td is suitable for the tree created by your browser, but it's not suitable for the file provided to XML::LibXML.

Upvotes: 4

Related Questions