in reply to Re: Ordering meta tags with HTML::Element
in thread Ordering meta tags with HTML::Element

The output is read by an automated process this is failing because it requires the meta tags to be name content

In that case you have a problem because this "automated process" is not accepting valid HTML and pretty much any of the pre-written modules you might use (HTML::Element included) will not care about attribute ordering as has been discussed by my fellow monks already.

The real fix here is to change the "automated process" to avoid the insistence on attribute ordering. Failling that, I guess you will need to abandon HTML::Element and fall back on hand-rolling the HTML. Good luck.

  • Comment on Re^2: Ordering meta tag attributes with HTML::Element

Replies are listed 'Best First'.
Re^3: Ordering meta tag attributes with HTML::Element
by HeadScratcher (Novice) on Jun 15, 2016 at 09:22 UTC
    I have no control over the automated process The only solution I have come up with is to do this but not very elegant I was hopping someone would have a better idea
    for my $m (sort keys %meta) { my $m_el = HTML::Element->new('meta'); $m_el->attr('0name',$m); $m_el->attr('1content',$meta{$m}); $head->push_content($m_el); } my $out = $body->as_HTML('<>&',' ',{}); $out=~s/0name/name/g; $out=~s/1content/content/g; print $out;

      If you look at the source code of HTML::Element, you will find that it prefers to sort the attribute keys alphabetically. My approach would be to derive a class from HTML::Element which overrides the to_string method (or whatever) to output the attributes in a more explicit manner. I think something like this was already suggested to you.

      Your approach of hoping that the text will never contain 0name or 1content is also plausible, personally, I would rather look at using one of the template solutions to create text that absolutely must follow a specific format.

        Your approach of hoping that the text will never contain 0name or 1content is also plausible...

        I went to http://www.w3schools.com/tags/tag_meta.asp. I clicked the "Try it Yourself >>" button. I coded up changes to the HTML.

        <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="description" content="Free Web tutorials"> <meta name="keywords" content="HTML,CSS,XML,JavaScript"> <meta name="author" content="Hege Refsnes"> <meta 0name="description" 1content="Free Web tutorials"> <meta 0name="keywords" 1content="HTML,CSS,XML,JavaScript"> <meta 0name="author" 1content="Hege Refsnes"> </head> <body> <p>All meta information goes in the head section...</p> </body> </html>

        I clicked the "See Result >>" button.

        It displayed with no complaints.

        I ran it through the HTML checker at https://validator.w3.org/.

        It complained.

        I stored the webpage on my disk and brought up the webpage in Firefox.

        It displayed the webpage.

        So, as long as one is creating their own HTML and can control the process, then the original poster's code at Re^3: Ordering meta tag attributes with HTML::Element will work.

        But, if one is spidering the Internet, who knows what garbage is out there?

      Maybe that "automated process" doesn't process your data, because you misspelt the content of the "Content" attribute of the first "meta" element?
      At least if a lookup is involved, it may simply not find "J K Rolling".
Re^3: Ordering meta tag attributes with HTML::Element
by Cow1337killr (Monk) on Jun 21, 2016 at 21:02 UTC
    ...hand-rolling the HTML

    Hahaha.

    Yeah. Maybe he can hire some of the neighborhood kids to help him. They probably are looking for something to do this summer.

    (Thank you for adding some humour to my day.)