in reply to Re^4: extracting data from HTML
in thread extracting data from HTML
The problem with this:
$xml->findnodes('//html/head/title')
... is that none of the names in the path include a namespace. (X)HTML elements are always namespaced. Hence my rather awkward...
$xml->findnodes('//*[local-name()="title"]')
i.e. "select all elements where the local part of the element's name is 'title'"
Another solution (arguably a lot more readable) would be to forcibly bind the XHTML namespace to a prefix:
$xml->documentElement->setNamespace( 'http://www.w3.org/1999/xhtml' => 'xh', );
And then you can freely use that prefix in XPaths.
$xml->findnodes('//xh:html/xh:head/xh:title')
This specific problem is mentioned in the XML::LibXML::Node documentation - look for the "NOTE ON NAMESPACES AND XPATH" in the documentation for the findnodes method.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: extracting data from HTML
by Jurassic Monk (Acolyte) on Jun 06, 2012 at 15:37 UTC |