Skip to content

Commit f1cf252

Browse files
adding start of namespace handling
1 parent 40809e0 commit f1cf252

File tree

12 files changed

+130
-14
lines changed

12 files changed

+130
-14
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/test_files/
22
demo.rb
3-
/spec/resources/actual.xml
3+
.temp.actual.xml
44

55
*.gem
66
*.rbc

lib/puppet_x/xdt_xpath_reader.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ def self.read(node)
1010
return xpath
1111
end
1212
def self.read_local(node)
13-
xpath = ''
14-
xpath += "#{node.namespace.prefix}:" unless node.namespace.nil?
15-
return xpath + node.name
13+
return node.name if node.namespace.nil?
14+
return "*[local-name()='#{node.name}' and namespace-uri()='#{node.namespace.href}']"
1615
end
1716
def self.read_attribute(attr)
1817
xpath = ''

spec/acceptance/puppet_x/xdt_transformer_spec.rb

+16-6
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,23 @@ def compare_elements(expected, actual)
3535

3636
describe XdtTransformer do
3737
describe "#transform" do
38-
context "when transforming spec/resources/source.xml with spec/resources/transform.xml" do
39-
it "transformed file same as spec/resources/expected.xml" do
40-
source_doc = Nokogiri::XML(File.read("#{Dir.pwd}/spec/resources/source.xml"))
41-
transform_doc = Nokogiri::XML(File.read("#{Dir.pwd}/spec/resources/transform.xml"))
42-
expected_doc = Nokogiri::XML(File.read("#{Dir.pwd}/spec/resources/expected.xml"))
38+
context "when transforming spec/resources/transform_only/source.xml with spec/resources/transform_only/transform.xml" do
39+
it "transformed file same as spec/resources/transform_only/expected.xml" do
40+
source_doc = Nokogiri::XML(File.read("#{Dir.pwd}/spec/resources/transform_only/source.xml"))
41+
transform_doc = Nokogiri::XML(File.read("#{Dir.pwd}/spec/resources/transform_only/transform.xml"))
42+
expected_doc = Nokogiri::XML(File.read("#{Dir.pwd}/spec/resources/transform_only/expected.xml"))
4343
transformed_doc = XdtTransformer.new(source_doc, transform_doc).transform
44-
File.write("#{Dir.pwd}/spec/resources/actual.xml", transformed_doc.to_s)
44+
File.write("#{Dir.pwd}/spec/resources/transform_only/.temp.actual.xml", transformed_doc.to_s)
45+
compare_elements(expected_doc.root, transformed_doc.root)
46+
end
47+
end
48+
context "when transforming spec/resources/transform_with_namespaces/source.xml with spec/resources/transform_with_namespaces/transform.xml" do
49+
it "transformed file same as spec/resources/transform_with_namespaces/expected.xml" do
50+
source_doc = Nokogiri::XML(File.read("#{Dir.pwd}/spec/resources/transform_with_namespaces/source.xml"))
51+
transform_doc = Nokogiri::XML(File.read("#{Dir.pwd}/spec/resources/transform_with_namespaces/transform.xml"))
52+
expected_doc = Nokogiri::XML(File.read("#{Dir.pwd}/spec/resources/transform_with_namespaces/expected.xml"))
53+
transformed_doc = XdtTransformer.new(source_doc, transform_doc).transform
54+
File.write("#{Dir.pwd}/spec/resources/transform_with_namespaces/.temp.actual.xml", transformed_doc.to_s)
4555
compare_elements(expected_doc.root, transformed_doc.root)
4656
end
4757
end

spec/resources/expected.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<root>
22
<transformonly>
3-
<basictransforms_nonamespace>
3+
<!-- <basictransforms_nonamespace>
44
<basictransforms_nonamespace_insertafter a="aaa" b="bbb" />
55
<basictransforms_nonamespace_insertafter_1 a="aaaa" c="cccc" />
66
<basictransforms_nonamespace_insertbefore_2 a="aaaa" c="cccc" />
@@ -18,5 +18,11 @@
1818
</basictransforms_nonamespace_setallattributes>
1919
<basictransforms_nonamespace_insert a="aaaa" c="cccc" />
2020
</basictransforms_nonamespace>
21+
<basictransforms_namespace xmlns:a="http://a/">
22+
<a:basictransforms_namespace_replace b="bbb" />
23+
</basictransforms_namespace> -->
24+
<b:basictransforms_parentnamespace xmlns:b="http://b/">
25+
<basictransforms_namespace_replace b="bbb" />
26+
</b:basictransforms_parentnamespace>
2127
</transformonly>
2228
</root>

spec/resources/source.xml

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<root>
22
<transformonly>
3-
<basictransforms_nonamespace>
3+
<!--<basictransforms_nonamespace>
44
<basictransforms_nonamespace_insertafter a="aaa" b="bbb" />
55
<basictransforms_nonamespace_insertbefore a="aaa" b="bbb" />
66
<basictransforms_nonamespace_remove a="aaa" b="bbb">
@@ -21,6 +21,9 @@
2121
<basictransforms_nonamespace_setallattributes a="aaa" b="bbb" >
2222
<basictransforms_nonamespace_setallattributes_1 />
2323
</basictransforms_nonamespace_setallattributes>
24-
</basictransforms_nonamespace>
24+
</basictransforms_nonamespace>-->
25+
<basictransforms_namespace xmlns:a="http://a/">
26+
<a:basictransforms_namespace_replace a="aaa" />
27+
</basictransforms_namespace>
2528
</transformonly>
2629
</root>

spec/resources/transform.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<root xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
22
<transformonly>
3-
<basictransforms_nonamespace>
3+
<!-- <basictransforms_nonamespace>
44
<basictransforms_nonamespace_insert a="aaaa" c="cccc" xdt:Transform="Insert" />
55
<basictransforms_nonamespace_insertafter_1 a="aaaa" c="cccc" xdt:Transform="InsertAfter(//root/transformonly/basictransforms_nonamespace/basictransforms_nonamespace_insertafter)" />
66
<basictransforms_nonamespace_insertbefore_2 a="aaaa" c="cccc" xdt:Transform="InsertBefore(//root/transformonly/basictransforms_nonamespace/basictransforms_nonamespace_insertbefore)" />
@@ -16,5 +16,11 @@
1616
<basictransforms_nonamespace_setattributes a="aaaa" c="cccc" xdt:Transform="SetAttributes(a,c)" />
1717
<basictransforms_nonamespace_setallattributes a="aaaa" c="cccc" xdt:Transform="SetAttributes(a,c)" />
1818
</basictransforms_nonamespace>
19+
<basictransforms_namespace xmlns:a="http://a/">
20+
<a:basictransforms_namespace_replace b="bbb" xdt:Transform="Replace" />
21+
</basictransforms_namespace> -->
22+
<b:basictransforms_parentnamespace xmlns:b="http://b/">
23+
<basictransforms_namespace_replace b="bbb" xdt:Transform="Replace" />
24+
</b:basictransforms_parentnamespace>
1925
</transformonly>
2026
</root>
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<root>
2+
<insertafter a="aaa" b="bbb" />
3+
<insertafter_1 a="aaaa" c="cccc" />
4+
<insertbefore_2 a="aaaa" c="cccc" />
5+
<insertbefore a="aaa" b="bbb" />
6+
<removeattributes />
7+
<replace a="aaaa" c="cccc">
8+
<replace_1 a="aaaa" c="cccc" />
9+
<replace_3 />
10+
</replace>
11+
<setattributes a="aaaa" b="bbb" c="cccc">
12+
<setattributes_1 />
13+
</setattributes>
14+
<setallattributes a="aaaa" b="bbb" c="cccc">
15+
<setallattributes_1 />
16+
</setallattributes>
17+
<insert a="aaaa" c="cccc" />
18+
</root>
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<root>
2+
<insertafter a="aaa" b="bbb" />
3+
<insertbefore a="aaa" b="bbb" />
4+
<remove a="aaa" b="bbb">
5+
<remove_1 />
6+
</remove>
7+
<removeall>
8+
<removeall_1 />
9+
<removeall_2 />
10+
</removeall>
11+
<removeattributes a="aaa" b="bbb" />
12+
<replace a="aaa" b="bbb">
13+
<replace_1 a="aaa" b="bbb" />
14+
<replace_2 />
15+
</replace>
16+
<setattributes a="aaa" b="bbb" >
17+
<setattributes_1 />
18+
</setattributes>
19+
<setallattributes a="aaa" b="bbb" >
20+
<setallattributes_1 />
21+
</setallattributes>
22+
</root>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<root xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
2+
<insert a="aaaa" c="cccc" xdt:Transform="Insert" />
3+
<insertafter_1 a="aaaa" c="cccc" xdt:Transform="InsertAfter(//root/insertafter)" />
4+
<insertbefore_2 a="aaaa" c="cccc" xdt:Transform="InsertBefore(//root/insertbefore)" />
5+
<remove a="aaaa" c="cccc" xdt:Transform="Remove" >
6+
<remove_2 />
7+
</remove>
8+
<removeall xdt:Transform="RemoveAll" />
9+
<removeattributes xdt:Transform="RemoveAttributes" />
10+
<replace a="aaaa" c="cccc" xdt:Transform="Replace">
11+
<replace_1 a="aaaa" c="cccc" />
12+
<replace_3 />
13+
</replace>
14+
<setattributes a="aaaa" c="cccc" xdt:Transform="SetAttributes(a,c)" />
15+
<setallattributes a="aaaa" c="cccc" xdt:Transform="SetAttributes(a,c)" />
16+
</root>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<root>
2+
<a:parent_with_prefix xmlns:a="http://prefix_namespace/">
3+
<a:child_with_prefix b="aaaa" />
4+
<child_without_prefix b="bbbb" />
5+
</a:parent_with_prefix>
6+
<parent_with_default_namespace xmlns="http://default_namespace/">
7+
<child_with_default_namespace a="aaaa" />
8+
</parent_with_default_namespace>
9+
<parent_without_namespace>
10+
<child_without_namespace a="aaaa" />
11+
</parent_without_namespace>
12+
</root>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<root>
2+
<a:parent_with_prefix xmlns:a="http://prefix_namespace/">
3+
<a:child_with_prefix a="aaa" />
4+
<child_without_prefix a="bbb" />
5+
</a:parent_with_prefix>
6+
<parent_with_default_namespace xmlns="http://default_namespace/">
7+
<child_with_default_namespace a="aaa" />
8+
</parent_with_default_namespace>
9+
<parent_without_namespace>
10+
<child_without_namespace a="aaa" />
11+
</parent_without_namespace>
12+
</root>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<root xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
2+
<a:parent_with_prefix xmlns:a="http://prefix_namespace/">
3+
<a:child_with_prefix b="aaaa" xdt:Transform="Replace" />
4+
<child_without_prefix b="bbbb" xdt:Transform="Replace" />
5+
</a:parent_with_prefix>
6+
<parent_with_default_namespace xmlns="http://default_namespace/">
7+
<child_with_default_namespace a="aaaa" xdt:Transform="Replace" />
8+
</parent_with_default_namespace>
9+
<parent_without_namespace>
10+
<child_without_namespace a="aaaa" xdt:Transform="Replace" />
11+
</parent_without_namespace>
12+
</root>

0 commit comments

Comments
 (0)