Attributes ignored when parsing an element with a child as text node
munshkr opened this issue · comments
I stumbled upon an element that has attributes and a simple text node, like this one:
<price currency="usd">300</price>
I was surprised to see the parsing result as:
{"price"=>"300"}
Then I found out this behaviour was intentional. Is there a good reason to ignore attributes in this case?
they are "hidden" in attributes
method.
result = Nori.new.parse('<price currency="usd">300</price>')
result['price'].attributes # => {"currency"=>"usd"}
@munshkr this is really related to the design of the hash that is created. the problem with converting xml to a ruby hash is that in xml an element can have attributes and children. a ruby hash just has keys which map to xml element names and values which map to the element's children. if you would add the attributes to that hash, you would need to somehow "namespace" them to properly query them and not run into conflicts with attributes which have the same name as child elements, etc.
so that's why attributes are not inside the hash, but available via the #attributes
method instead.
@robuye thanks for helping out!