Reputation: 61
This is my XML:
<response errors="0"><person><middlename/><name>Egor</name><carsList><car default="true">0777AD</car></carsList><surname>Petrov</surname></person><funds>505.56</funds></response>
I need to get the value of the default
attribute for the <car>
element.
I found some solutions on Stack Overflow with attr()
and attributue()
but I had no success using them.
My code is:
unless @account.xpath("//person//carslist").blank?
@account.xpath("//person//carslist").each do |car|
p car.attribute('default')
end
end
On my console I see nil but need to see true.
The correctly variant is:
unless @account.xpath("//person//carsList/*").blank?
@account.xpath("//person//carsList/*").each do |car|
p car.attribute('default').content
end
end
What can it be?
Upvotes: 0
Views: 786
Reputation: 10394
You want:
unless @account.xpath("//person//carsList/*").blank?
Notice the capital 'L' in carsList
instead of carslist
. Also note the /*
to get the child nodes of carList
.
The corrected code would be:
unless @account.xpath("//person//carsList/*").blank?
@account.xpath("//person//carsList").each do |car|
p car.attribute('default')
end
end
Upvotes: 1