Nokogiri : Modifying Nodes and Attributes

Discussion in 'Ruby' started by Une Bévue, May 31, 2011.

  1. Une Bévue

    Une Bévue Guest

    In my test i do have :
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    Because i know this declaration is false from Lynx saying it is UTF-8, i
    want to change the content attribute by :
    meta['content']="text/html; charset=UTF-8" if !meta.nil? &&

    however, printing meta gives always :
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    that's to say, no change at all.

    Is this behaviour implied by the fact the meta tag isn't self closed, ie
    not ending by " />" ???

    If yes, in that case i could unling all meta tags and create a good one
    No quicker solution ?
    Une Bévue, May 31, 2011
    1. Advertisements

  2. Une Bévue

    Une Bévue Guest

    This doesn't work too :
    metas.each {|x| x.unlink} "meta", doc
    meta['content']="text/html; charset=UTF-8"

    printing head tag gives :
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link href="..." type="text/css">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    then, i got back 2 times the meta tag, the older one and the new one -

    my first Nokogiri line being :
    doc=Nokogiri::HTML(html) { |config| config.noblanks.noent }

    the original html starting with :

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    <html xmlns="">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"

    where the meta tag is well balanced by " />" ...
    Une Bévue, May 31, 2011
    1. Advertisements

  3. Have you tried using `Document#meta_encoding=3D` ?


    Also, prefer nokogiri-talk to ruby-talk for Nokogiri questions. Thank you!
    Mike Dalessio, May 31, 2011
  4. Une Bévue

    Une Bévue Guest

    No, I'll look at...

    I did a workaround in between, using a gsub...
    OK, i've suscribed to this list.
    Une Bévue, May 31, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.