Skip to content

Commit

Permalink
Nokogiri parser ready for same attr and inner element
Browse files Browse the repository at this point in the history
  • Loading branch information
ka8725 committed Jul 9, 2013
1 parent 44fc05f commit a28ed86
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/multi_xml/parsers/libxml2_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ def node_to_hash(node, hash={})
end

# Handle attributes
each_attr(node) {|a| node_hash[node_name(a)] = a.value }
each_attr(node) do |a|
key = node_name(a)

node_hash[key] = if v = node_hash[key]
[a.value, v]
else
a.value
end
end

hash
end
Expand Down
10 changes: 10 additions & 0 deletions spec/parser_shared_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@
end
end

context "element with the same inner element and attribute name" do
before do
@xml = "<user name='John'><name>Smith</name></user>"
end

it "returns nams as Array" do
expect(MultiXml.parse(@xml)['user']['name']).to eq ['John', 'Smith']
end
end

context "with content" do
before do
@xml = '<user>Erik Michaels-Ober</user>'
Expand Down

0 comments on commit a28ed86

Please sign in to comment.