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 8200689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:32:08+00:00 2026-06-07T06:32:08+00:00

Feels like I’m missing something pretty obvious here but can’t see it. I have

  • 0

Feels like I’m missing something pretty obvious here but can’t see it.

I have an XML file and are using the Nokogiri gem.

The XML looks like this (imagine if you will an infinite amount of ‘variants’):

<?xml version="1.0" encoding="UTF-8"?> 
<products> 
    <variant> 
        <sku type="string">123abc</sku>
        <inventory-quantity type="integer">68</inventory-quantity> 
    </variant> 
    <variant> 
        <sku type="string">321cba</sku>
        <inventory-quantity type="integer">22</inventory-quantity> 
    </variant>
</products>

I want to loop over the variants and create a corresponding record for each that contains the ‘sku’ and ‘inventory-quantity’ attributes.

This is what I’ve got so far, but instead of creating individual records, in the above case it creates two records and inserts the complete array or NodeSet that Nokogiri returns into each records attribute. So this:

doc = Nokogiri::XML(File.open("#{Rails.root}/public/new.xml")) 

variant = doc.xpath("//variant")

variant.each do |product| 

  sku = product.xpath("//sku").text
  quan = product.xpath("//inventory-quantity").text

  Productmapping.create(:sku => sku, :product_quantity => quan)

end

creates this…

sku              inventory-quantity

123abc321cba     6822
123abc321cba     6822

Where I actually want:

sku              inventory-quantity

123ab            68
321cba           22

It looks like it’s because the xpath locator returns all occurrences into an array and I’m calling that array and inserting it for each record.

Thanks!

  • 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-06-07T06:32:10+00:00Added an answer on June 7, 2026 at 6:32 am

    The answer is simple – instead of:

    sku = product.xpath("//sku").text
    quan = product.xpath("//inventory-quantity").text
    

    just use:

    sku = product.xpath("sku").text
    quan = product.xpath("inventory-quantity").text
    

    It’s because //sku selects all the sku descendants of the document root.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This feels like im missing something obvious, but i've been reading tutorials for 3
This feels like a beginner question with a pretty obvious answer, but I can't
I have what feels like a simple problem, but I can't seem to find
This feels like an extremely n00b question, but here goes... I have a series
This feels like a stupid question, but I can't seem to see the answer.
Have I lost it, but if feels like I remember doing something along the
This feels like a really basic question, but I can't figure it out anyway..
This feels like it should be pretty simple, but not much seems to be
this question feels like it would have been asked already, but I've not found
This feels like a dumb question, but I see the following in the Oracle

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.