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.
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.