Nokogiri parse xml help

I’m fairly new to ruby and Nokogiri, and have come upon my first
problem. The xml file I am trying to parse has all the data contained in
attributes. I have basically found how to build the string to insert
into the text file. Could someone possibly help me out.

I have this xml file:

<ig:prescribed_item class_ref=“0161-1#01-765557#1”>
<ig:prescribed_property property_ref=“0161-1#02-016058#1”
is_required=“false” combination_allowed=“false” one_of_allowed=“false”>
<dt:measure_number_type representation_ref=“0161-1#04-000005#1”>
dt:real_type
<dt:real_format pattern="\d(1,).\d(1,)"/>
</dt:real_type>
<dt:prescribed_unit_of_measure
UOM_ref=“0161-1#05-003260#1”/>
</dt:measure_number_type>
</ig:prescribed_property>
<ig:prescribed_property property_ref=“0161-1#02-016059#1”
is_required=“false” combination_allowed=“false” one_of_allowed=“false”>
<dt:measure_number_type representation_ref=“0161-1#04-000005#1”>
dt:real_type
<dt:real_format pattern="\d(1,).\d(1,)"/>
</dt:real_type>
<dt:prescribed_unit_of_measure
UOM_ref=“0161-1#05-003260#1”/>
</dt:measure_number_type>
</ig:prescribed_property>
</ig:prescribed_item>
</ig:identification_guide>

And I want to parse it into a text file like this with the class ref
duplicated for each property:

class_ref|property_ref|is_required|UOM_ref
0161-1#01-765557#1|0161-1#02-016058#1|false|0161-1#05-003260#1
0161-1#01-765557#1|0161-1#02-016059#1|false|0161-1#05-003260#1

This is the code I have so far:

require ‘nokogiri’

doc =
Nokogiri::XML(File.open(“C:/_Chris/ECCMA/eOTD/1_xml/xml/simple_example.xml”),
‘UTF-8’) do |config|
config.strict
end

myarray = []

content = doc.xpath("//ig:prescribed_item").each {|i|
i.xpath("//ig:prescribed_item/@class_ref").each { |clas|
myarray.push(clas.text)
clas.xpath("//ig:prescribed_property/@property_ref").each { |prop|
myarray.push(prop.text)
prop.xpath("//dt:measure_number_type/@representation_ref").each {
|repref| myarray.push(repref.text)
repref.xpath("//dt:prescribed_unit_of_measure/@UOM_ref").each
{|uom| myarray.push(uom.text)}

  }


}

}
}

result of this code is
:“0161-1#01-765557#1|0161-1#02-016058#1|0161-1#04-000005#1|0161-1#05-003260#1|0161-1#05-003260#1|0161-1#04-000005#1|0161-1#05-003260#1|0161-1#05-003260#1|0161-1#02-016059#1|0161-1#04-000005#1|0161-1#05-003260#1|0161-1#05-003260#1|0161-1#04-000005#1|0161-1#05-003260#1|0161-1#05-003260#1”

Could you help me make it as :

class_ref|property_ref|is_required|UOM_ref
0161-1#01-765557#1|0161-1#02-016058#1|false|0161-1#05-003260#1
0161-1#01-765557#1|0161-1#02-016059#1|false|0161-1#05-003260#1

Thank You

Chris R. wrote in post #1006754:

And I want to parse it into a text file like this with the class ref
duplicated for each property:

Change all the tag names in your xml to A, B, C, etc. Change all the
names of the attributes to a1, a2, a3, etc. Change all the values of
the attributes to 1, 2, 3, etc. Then repost your code.

In other words, when you ask for help, you need to simplify your
code/data so anyone can easily read it.

hash = {
‘A’ => {‘a1’ => {‘x’ => 11, ‘y’ => 22},
‘a2’ => {‘x’ => 33, ‘y’ => 44}
},
‘B’ => {‘b1’ => {‘x’ => 55, ‘y’ => 66},
‘b2’ => {‘x’ => 77, ‘y’ => 88}
}
}

hash.each do |capitalized_key, subhash|
subhash.each do |ab_key, subsubhash|
print capitalized_key
print ’ ', ab_key
subsubhash.each do |xy_key, num|
if xy_key == ‘x’
print ’ ', num
end
end
puts
end
end

–output:–
A a1 11
A a2 33
B b1 55
B b2 77