Trying to get an attribute and element at the same time

101 views
Skip to first unread message

Sara T

unread,
Oct 20, 2010, 12:50:56 PM10/20/10
to happymapper
Having a heck of a time with this one.

I have a node that looks like this:

<ItemInfo>
<ProductVar text_id="Variable1">MyValue</ProductVar>
<ProductVar text_id="Variable2">MySecondValue</ProductVar>
</ItemInfo>

I need both the value of the attribute "text_id" and the element value
"MyValue". I was trying to use something like what I saw in the
"current weather" example:

<aws:current-condition icon="http://deskwx.weatherbug.com/images/
Forecast/icons/cond007.gif">Sunny</aws:current-condition>
element :current_condition, String, :tag => 'current-
condition', :attributes => {:icon => String}

Which (the example) works just fine on my system. So this is the
mapping I have:

class ItemInfo
include HappyMapper
tag 'ItemInfo'
has_many :product_var, String, :tag => "ProductVar", :attributes =>
{:text_id => String}
end

This does not return text_id. I can return
item_info.product_var.text_id if I use "element" in place of
"has_many", but there are always going to be multiple "ProductVar"s,
so that won't work. Whenever I try to replace "element" with
"has_many", I get this error:

undefined method `attribute_nodes' for ["text_id", "Variable1"]:Array

Ideas?

Andrew N

unread,
Nov 5, 2010, 3:51:41 AM11/5/10
to happymapper
Hi,

I had same issue this is supported by using the content method.

I think what you want is something like this:

class ProductVar
include HappyMapper
tag "ProductVar"
content "value"
attribute :text_id, String
end

class ItemInfo
include HappyMapper
tag 'ItemInfo'
has_many :product_var, ProductVar
end

So now.... iteminfo.product_var[0].value and
iteminfo.product_var.text_id should both work.

cya
Andrew
Reply all
Reply to author
Forward
0 new messages