Skip to content

Commit

Permalink
added delete_namespace_attributes flag to remove namespace attributes…
Browse files Browse the repository at this point in the history
… such as xmlsn:* or xsi:*
  • Loading branch information
cloocher committed Apr 22, 2013
1 parent 23c47e0 commit 8193a87
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage
*.gem
.bundle
Gemfile.lock
/.idea
9 changes: 5 additions & 4 deletions lib/nori.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class Nori

def initialize(options = {})
defaults = {
:strip_namespaces => false,
:convert_tags_to => nil,
:advanced_typecasting => true,
:parser => :nokogiri
:strip_namespaces => false,
:delete_namespace_attributes => false,
:convert_tags_to => nil,
:advanced_typecasting => true,
:parser => :nokogiri
}

validate_options! defaults.keys, options.keys
Expand Down
2 changes: 2 additions & 0 deletions lib/nori/xml_utility_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def initialize(options, name, normalized_attributes = {})
@nil_element = attributes.delete(key) == "true"
attributes.delete("xmlns:#{result[2]}") if result[1]
end
attributes.delete(key) if @options[:delete_namespace_attributes] && key[/^(xmlns|xsi)/]
end

@attributes = undasherize_keys(attributes)
@children = []
@text = false
Expand Down
22 changes: 22 additions & 0 deletions spec/nori/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@
end
end

context "#parse without :delete_namespace_attributes" do
it "can be changed to not delete namespace attributes" do
xml = '<userResponse xmlns="http://schema.company.com/some/path/to/namespace/v1"><accountStatus>active</accountStatus></userResponse>'
hash = nori(:delete_namespace_attributes => false).parse(xml)
hash.should == {"userResponse" => {"@xmlns" => "http://schema.company.com/some/path/to/namespace/v1", "accountStatus" => "active"}}
end
end

context "#parse with :delete_namespace_attributes" do
it "can be changed to delete xmlns namespace attributes" do
xml = '<userResponse xmlns="http://schema.company.com/some/path/to/namespace/v1"><accountStatus>active</accountStatus></userResponse>'
hash = nori(:delete_namespace_attributes => true).parse(xml)
hash.should == {"userResponse" => {"accountStatus" => "active"}}
end

it "can be changed to delete xsi namespace attributes" do
xml = '<userResponse xsi="abs:myType"><accountStatus>active</accountStatus></userResponse>'
hash = nori(:delete_namespace_attributes => true).parse(xml)
hash.should == {"userResponse" => {"accountStatus" => "active"}}
end
end

def nori(options = {})
Nori.new(options)
end
Expand Down

0 comments on commit 8193a87

Please sign in to comment.