in reply to Namespace error while parsing html document.

It's not HTML. HTML doesn't even have namespaces. It claims to be XHTML, so you should be using parse_file.

Unfortunately, it's not well-formed XHTML. Lots of unescaped ampersands and less-than symbols, for starters.

The following fixes the escaping errors, but there's still a bunch of duplicate ids and unmatched tags:

#!/usr/bin/perl -w use strict; use warnings; use XML::LibXML qw( ); use LWP::UserAgent qw( ); sub text_to_xml { my ($text) = @_; $text =~ s/&/&amp;/g; $text =~ s/</&lt;/g; return $text; } my $url = "http://www.marketwatch.com/story/brazil-mexico-stocks-face- +quarterly-slides-2010-06-30?siteid=rss&rss=1"; my $ua = LWP::UserAgent->new(); my $response = $ua->get($url); $response->is_success() or die "Request failed: ". $response->status_line() ."\n"; my $xhtml = $response->decoded_content(charset => "none"); $xhtml =~ s{(<script[^>]*>)(.*?)(</script>)}{"$1".text_to_xml("$2")."$ +3"}sieg; $xhtml =~ s/&(?!#|[a-zA-Z0-9]{1,8};)/&amp;/g; my $parser = XML::LibXML->new; my $doc = $parser->parse_string($xhtml);

Remaining errors

There's a parser option that directs libxml to recover from errors if possible. It might help. If not, HTML::Parser is designed to handle garbage.

Replies are listed 'Best First'.
Re^2: Namespace error while parsing html document.
by mr_p (Scribe) on Jul 01, 2010 at 01:07 UTC
    Thanks for your help.

    surprised the HTML is this bad. Browser renders it pretty good compare to LibXml.

      Browsers are very forgiving. LibXML expects XML or HTML. I suggested two options.
Re^2: Namespace error while parsing html document.
by mr_p (Scribe) on Jul 01, 2010 at 13:25 UTC

    Do you know why I was not able to pass $content->{_content} to parse_html_string(), It was blank. Isn't $content->{_content} suppose to have the the file downloaded?

    Also,

    The only reason I need to load the html page is because I wanted to parse stylesheet link out. For Instance:

    Can I use RegExp to parse the below string? I would need to parse all three hrefs in to array.

    <?xml-stylesheet href="perl1.css" type="text/css"?> <link href="//www.perl.org/css/perl1.css" rel="stylesheet"> <link href="/css/perl.css" rel="stylesheet">