Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

The doctype is stored as an attribute of the root element where the attribute name is "_decl" and the value is an HTML::Element object, so basically, you want

my $ele = $root->look_down( _decl => ...was specified..., ) or die qq{declaration not found\n}; my $dec = $ele->attr('_decl');

Since look_down doesn't allow us to check if an attribute was specified we'll have to provide our own handler.

my $ele = $root->look_down( sub { $_[0]->attr('_decl') } ) or die qq{declaration not found\n}; my $dec = $ele->attr('_decl');

But why use look_down at all? The only possible node it could return is the root node. The above code boils down to

my $dec = $root->attr('_decl') or die qq{declaration not found\n};

Now that we have the declaration, let's move on to changing it. It makes no sense to use splice_content to modify attributes. attr is the proper method.

$dec->attr(text => 'DOCTYPE html PUBLIC "HTML4"');

Since the entire purpose is to replace the declaration, let's create a new declaration rather than dying if it's absent.

$root->attr('_decl', HTML::Element->new('~declaration', text => 'DOCTYPE html PUBLIC "HTML4"', ) );

All together:

#!/usr/bin/perl use warnings; use strict; use HTML::TreeBuilder; my $content = do{local $/;<DATA>}; my $root = HTML::TreeBuilder->new_from_content($content); $root->attr('_decl', HTML::Element->new('~declaration', text => 'DOCTYPE html PUBLIC "HTML4"', ) ); print $root->as_HTML; __DATA__ <!DOCTYPE html PUBLIC "XHTML"> <html> <head><title>declaration</title></head> <body><p>declaration</p></body> </html>
<!DOCTYPE html PUBLIC "HTML4"> <html><head><title>declaration</title></head><body><p>declaration</bod +y></html>

In reply to Re: Using HTML::TreeBuilder to change the DOCTYPE declaration by ikegami
in thread Using HTML::TreeBuilder to change the DOCTYPE declaration by wfsp

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (7)
    As of 2020-07-08 14:47 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found