Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7648095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:37:38+00:00 2026-05-31T10:37:38+00:00

I want to extract all the HTML5 data attributes from a tag, just like

  • 0

I want to extract all the HTML5 data attributes from a tag, just like this jQuery plugin.

For example, given:

<span data-age="50" data-location="London" class="highlight">Joe Bloggs</span>

I want to get a hash like:

{ 'data-age' => '50', 'data-location' => 'London' }

I was originally hoping use a wildcard as part of my CSS selector, e.g.

Nokogiri(html).css('span[@data-*]').size

but it seems that isn’t supported.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:37:40+00:00Added an answer on May 31, 2026 at 10:37 am

    Option 1: Grab all data elements

    If all you need is to list all the page’s data elements, here’s a one-liner:

    Hash[doc.xpath("//span/@*[starts-with(name(), 'data-')]").map{|e| [e.name,e.value]}]
    

    Output:

    {"data-age"=>"50", "data-location"=>"London"}
    

    Option 2: Group results by tag

    If you want to group your results by tag (perhaps you need to do additional processing on each tag), you can do the following:

    tags = []
    datasets = "@*[starts-with(name(), 'data-')]"
    
    #If you want any element, replace "span" with "*"
    doc.xpath("//span[#{datasets}]").each do |tag|
        tags << Hash[tag.xpath(datasets).map{|a| [a.name,a.value]}]
    end
    

    Then tags is an array containing key-value hash pairs, grouped by tag.

    Option 3: Behavior like the jQuery datasets plugin

    If you’d prefer the plugin-like approach, the following will give you a dataset method on every Nokogiri node.

    module Nokogiri
      module XML
        class Node
          def dataset
            Hash[self.xpath("@*[starts-with(name(), 'data-')]").map{|a| [a.name,a.value]}]
          end
        end
      end
    end
    

    Then you can find the dataset for a single element:

    doc.at_css("span").dataset
    

    Or get the dataset for a group of elements:

    doc.css("span").map(&:dataset)
    

    Example:

    The following is the behavior of the dataset method above. Given the following lines in the HTML:

    <span data-age="50" data-location="London" class="highlight">Joe Bloggs</span>
    <span data-age="40" data-location="Oxford" class="highlight">Jim Foggs</span>
    

    The output would be:

    [
     {"data-location"=>"London", "data-age"=>"50"},
     {"data-location"=>"Oxford", "data-age"=>"40"}
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want to know a easy way to extract all data with same
I want extract the path of images from a html page using PHP-preg_match_all(), the
I've connected to database, using data set .xsr now I wan't to extract all
I want to extract only the leaf nodes from an XML document (i.e., only
I want to extract just the file name portion of a path. My code
I want to extract valid(on the basis of format) mobile numbers from a text.
I want to extract information from a web page. The page has m nodes,
I want to pull in data using a XML::XPath parser from a XML DB
I'm creating a html-layout based on an existing layout. I want to extract all
I've got a HTML code stored in string and I want to extract all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.