rsxml 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/rsxml.rb +34 -2
- data/spec/rsxml_spec.rb +11 -0
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/rsxml.rb
CHANGED
@@ -16,8 +16,16 @@ module Rsxml
|
|
16
16
|
# convert an XML string to an s-expression representation
|
17
17
|
# Rsxml.to_rsxml('<Foo foofoo="10"><Bar>barbar</Bar><Baz></Baz></Foo>')
|
18
18
|
# => ["Foo", {"foofoo"=>"10"}, ["Bar", "barbar"], ["Baz"]]
|
19
|
-
|
20
|
-
|
19
|
+
#
|
20
|
+
# if <tt>ns_prefixes</tt> is a Hash, then +doc+ is assumed to be a
|
21
|
+
# fragment, and is wrapped in an element with namespace declarations
|
22
|
+
# according to +ns_prefixes+
|
23
|
+
# fragment = '<foo:Foo foo:foofoo="10"><Bar>barbar</Bar><Baz></Baz></Foo>'
|
24
|
+
# Rsxml.to_rsxml(fragment, {"foo"=>"http://foo.com/foo", ""=>"http://baz.com/baz"})
|
25
|
+
# => ["foo:Foo", {"foo:foofoo"=>"10", "xmlns:foo"=>"http://foo.com/foo", "xmlns"=>"http://baz.com/baz"}, ["Bar", "barbar"], ["Baz"]]
|
26
|
+
def to_rsxml(doc, ns_prefixes=nil)
|
27
|
+
doc = Xml.wrap_fragment(doc, ns_prefixes)
|
28
|
+
root = Xml.unwrap_fragment(Nokogiri::XML(doc).children.first)
|
21
29
|
Xml.read_xml(root, [])
|
22
30
|
end
|
23
31
|
|
@@ -55,6 +63,30 @@ module Rsxml
|
|
55
63
|
module Xml
|
56
64
|
module_function
|
57
65
|
|
66
|
+
WRAP_ELEMENT = "RsxmlXmlWrapper"
|
67
|
+
|
68
|
+
def wrap_fragment(fragment, ns_prefixes)
|
69
|
+
return fragment if !ns_prefixes
|
70
|
+
|
71
|
+
ns_attrs = Hash[*ns_prefixes.map do |prefix,href|
|
72
|
+
prefix = nil if prefix.length == 0
|
73
|
+
[["xmlns", prefix].compact.join(":"), href]
|
74
|
+
end.flatten]
|
75
|
+
xml = Builder::XmlMarkup.new
|
76
|
+
xml.__send__(WRAP_ELEMENT, ns_attrs) do
|
77
|
+
xml << fragment
|
78
|
+
end
|
79
|
+
xml.target!
|
80
|
+
end
|
81
|
+
|
82
|
+
def unwrap_fragment(node)
|
83
|
+
if node.name==WRAP_ELEMENT
|
84
|
+
node.children.first
|
85
|
+
else
|
86
|
+
node
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
58
90
|
def read_xml(node, ns_stack)
|
59
91
|
prefix = node.namespace.prefix if node.namespace
|
60
92
|
tag = node.name
|
data/spec/rsxml_spec.rb
CHANGED
@@ -99,5 +99,16 @@ describe Rsxml do
|
|
99
99
|
it "should parse a doc with namespaces" do
|
100
100
|
test_roundtrip(["foo:foofoo", {"xmlns:foo"=>"http://foo.com/foo", "foo:bar"=>"1", "foo:baz"=>"baz"}])
|
101
101
|
end
|
102
|
+
|
103
|
+
it "should allow namespace prefixes to be specified when parsing a fragment" do
|
104
|
+
org = ["foo:foofoo", {"foo:bar"=>"1", "foo:baz"=>"baz"}]
|
105
|
+
xml = Rsxml.to_xml(["foo:foofoo", {"foo:bar"=>"1", "foo:baz"=>"baz"}])
|
106
|
+
|
107
|
+
org_with_ns = ["foo:foofoo", {"foo:bar"=>"1", "foo:baz"=>"baz", "xmlns"=>"http://baz.com/baz", "xmlns:foo"=>"http://foo.com/foo"}]
|
108
|
+
rsxml = Rsxml.to_rsxml(xml, {"foo"=>"http://foo.com/foo", ""=>"http://baz.com/baz"})
|
109
|
+
|
110
|
+
rsxml.should == org_with_ns
|
111
|
+
end
|
112
|
+
|
102
113
|
end
|
103
114
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rsxml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Trampoline Systems Ltd
|