port from REXML to Nokogiri by @cicatriz