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