改变
row.at('RoomTax').text,
到
row.at('RoomRate').attribute('RoomTax').text
require 'nokogiri'
require 'open-uri'
# doc = Nokogiri::XML(File.read("data.xml"))
data_xml = <<-EOT
<HotelRoomAvailabilityResponse xmlns="http://example.com">
<Status>
<StatusCode>01</StatusCode>
<Description>Successful: AvailableHotelRoom Successful</Description>
</Status>
<ResultIndex>1</ResultIndex>
<HotelRooms>
<HotelRoom>
<RoomIndex>1</RoomIndex>
<RoomTypeName>Superior - Double</RoomTypeName>
<Inclusion/>
<RoomTypeCode>x1</RoomTypeCode>
<RatePlanCode>y1</RatePlanCode>
<RoomRate IsPackageRate="false" B2CRates="false" AgentMarkUp="0.00" Currency="USD" RoomFare="87.54" RoomTax="18.36" PrefPrice="1412040.00" TotalFare="105.90" PrefCurrency="IDR">
<DayRates>
<DayRate Date="2018-12-30T00:00:00" BaseFare="29.17950000"/>
<DayRate Date="2018-12-31T00:00:00" BaseFare="29.17950000"/>
<DayRate Date="2019-01-01T00:00:00" BaseFare="29.17950000"/>
</DayRates>
<ExtraGuestCharges>0</ExtraGuestCharges>
<ChildCharges>0</ChildCharges>
<Discount>0</Discount>
<OtherCharges>0</OtherCharges>
<ServiceTax>0</ServiceTax>
</RoomRate>
<RoomPromtion/>
<Amenities/>
</HotelRoom>
<HotelRoom>
<RoomIndex>2</RoomIndex>
<RoomTypeName>Superior - Double</RoomTypeName>
<Inclusion/>
<RoomTypeCode>x2</RoomTypeCode>
<RatePlanCode>y2</RatePlanCode>
<RoomRate IsPackageRate="false" B2CRates="false" AgentMarkUp="0.00" Currency="USD" RoomFare="87.54" RoomTax="18.36" PrefPrice="1412040.00" TotalFare="105.90" PrefCurrency="IDR">
<DayRates>
<DayRate Date="2018-12-30T00:00:00" BaseFare="29.17950000"/>
<DayRate Date="2018-12-31T00:00:00" BaseFare="29.17950000"/>
<DayRate Date="2019-01-01T00:00:00" BaseFare="29.17950000"/>
</DayRates>
<ExtraGuestCharges>0</ExtraGuestCharges>
<ChildCharges>0</ChildCharges>
<Discount>0</Discount>
<OtherCharges>0</OtherCharges>
<ServiceTax>0</ServiceTax>
</RoomRate>
<RoomPromtion/>
<Amenities/>
</HotelRoom>
</HotelRooms>
</HotelRoomAvailabilityResponse>
EOT
doc = Nokogiri::XML(data_xml)
status_group = doc.xpath('//source:HotelRooms/*', 'source' => 'http://example.com')
row_hash = status_group.map{ |row|
{
RoomTypeName: row.at('RoomTypeName').text,
RoomTax: row.at('RoomRate').attribute('RoomTax').text,
}
}
puts row_hash
#=>
#{:RoomTypeName=>"Superior - Double", :RoomTax=>"18.36"}
#{:RoomTypeName=>"Superior - Double", :RoomTax=>"18.36"}