xml-smart 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/AUTHORS +4 -0
  2. data/COPYING +504 -0
  3. data/README.rdoc +55 -0
  4. data/Rakefile +15 -0
  5. data/example/EXAMPLE.xml +18 -0
  6. data/example/xpath_visual.rb +54 -0
  7. data/lib/xml/XSDtoRNG.xsl +448 -0
  8. data/lib/xml/smart.rb +177 -0
  9. data/lib/xml/smart_dom.rb +118 -0
  10. data/lib/xml/smart_domattribute.rb +34 -0
  11. data/lib/xml/smart_domattributeset.rb +62 -0
  12. data/lib/xml/smart_domelement.rb +213 -0
  13. data/lib/xml/smart_domnamespace.rb +32 -0
  14. data/lib/xml/smart_domnamespaceset.rb +125 -0
  15. data/lib/xml/smart_domnodeset.rb +59 -0
  16. data/lib/xml/smart_domother.rb +16 -0
  17. data/lib/xml/smart_domtext.rb +25 -0
  18. data/lib/xml/smart_qname.rb +34 -0
  19. data/test/1.xml +1 -0
  20. data/test/2.xml +1 -0
  21. data/test/3.xml +1 -0
  22. data/test/EXAMPLE-NS.xml +18 -0
  23. data/test/EXAMPLE-NSE.xml +18 -0
  24. data/test/EXAMPLE.str.xml +6 -0
  25. data/test/EXAMPLE.str.xml.test +6 -0
  26. data/test/EXAMPLE.tmp.xml +4 -0
  27. data/test/EXAMPLE.tmp.xml.test +4 -0
  28. data/test/EXAMPLE.xml +17 -0
  29. data/test/HELLO-MORE.xml +1 -0
  30. data/test/HELLO.rng +9 -0
  31. data/test/HELLO.xml +8 -0
  32. data/test/HELLO.xsd +16 -0
  33. data/test/XSL_BASE.xml +17 -0
  34. data/test/XSL_BASE.xml.test +16 -0
  35. data/test/XSL_DOCUMENT.xml +48 -0
  36. data/test/concurrent.xml +1 -0
  37. data/test/smartrunner.rb +25 -0
  38. data/test/tc_add.rb +21 -0
  39. data/test/tc_basic.rb +108 -0
  40. data/test/tc_concurrent.rb +73 -0
  41. data/test/tc_copy.rb +41 -0
  42. data/test/tc_create.rb +32 -0
  43. data/test/tc_delete.rb +52 -0
  44. data/test/tc_move_elements.rb +24 -0
  45. data/test/tc_namespace_default.rb +70 -0
  46. data/test/tc_namespace_detailed.rb +44 -0
  47. data/test/tc_namespace_find.rb +22 -0
  48. data/test/tc_nested.rb +26 -0
  49. data/test/tc_qname.rb +30 -0
  50. data/test/tc_relaxng.rb +30 -0
  51. data/test/tc_set_or_replace.rb +57 -0
  52. data/test/tc_sort.rb +18 -0
  53. data/test/tc_string.rb +41 -0
  54. data/test/tc_todoc.rb +27 -0
  55. data/test/tc_write.rb +19 -0
  56. data/test/tc_xinclude.rb +26 -0
  57. data/test/tc_xmlschema.rb +30 -0
  58. data/test/tc_xpath.rb +16 -0
  59. data/test/tc_xpath_attrs.rb +24 -0
  60. data/test/tc_xpath_functions.rb +12 -0
  61. data/test/tc_xpath_root.rb +23 -0
  62. data/test/tc_xsl.rb +22 -0
  63. data/xml-smart.gemspec +26 -0
  64. metadata +201 -0
@@ -0,0 +1,70 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
4
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
5
+
6
+ class TestNamespaceDefault < MiniTest::Unit::TestCase
7
+ def test_namespace_default
8
+ # No closure, so changes are temporary
9
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE.xml")
10
+ assert(doc.register_namespace('x','http://schemas.xmlsoap.org/wsdl/soap/') == true)
11
+ assert(doc.register_namespace('y','aaaa') == false)
12
+ assert(doc.unregister_namespace('soap') == false)
13
+
14
+ assert(doc.namespaces.length == 2)
15
+ assert(doc.namespaces.key? 'soap')
16
+ assert(doc.namespaces.key? 'x')
17
+
18
+ # add an element hallo to root
19
+ nsnode = doc.root.add("hallo")
20
+
21
+ success = true
22
+ nsnode.add('aaa:test') rescue success = false
23
+ assert(!success)
24
+
25
+ assert(nsnode.dump == "<hallo/>")
26
+ assert(!nsnode.namespace?)
27
+
28
+ success = true
29
+ n = nsnode.add('x:test') rescue success = false
30
+ assert(success)
31
+
32
+ assert(nsnode.dump == "<hallo>\n <soap:test/>\n</hallo>")
33
+
34
+ assert(nsnode.qname.name == "hallo")
35
+ assert(nsnode.qname.to_s == "hallo")
36
+ assert(nsnode.qname.prefix == nil)
37
+ assert(nsnode.qname.href == nil)
38
+ assert(nsnode.namespace? == false)
39
+ assert(nsnode.namespace == nil)
40
+
41
+ assert(n.qname.name == "test")
42
+ assert(n.qname.to_s == "soap:test")
43
+ assert(n.qname.prefix == "soap")
44
+ assert(n.qname.href == "http://schemas.xmlsoap.org/wsdl/soap/")
45
+ assert(n.namespace? == true)
46
+ assert(n.namespace.instance_of?(XML::Smart::Dom::Namespace) == true)
47
+ assert(n.namespace.prefix == "soap")
48
+ assert(n.namespace.href == "http://schemas.xmlsoap.org/wsdl/soap/")
49
+
50
+ # add a namespace to the element
51
+ nsnode.namespaces.add("test","http://heise1")
52
+ nsnode.namespaces.add("aaa","http://heise2")
53
+ nsnode.namespaces.add(nil,"http://heise")
54
+ nsnode.namespaces[nil] = "http://heiser"
55
+
56
+ assert(nsnode.namespaces[nil].prefix == 'xmlns')
57
+ assert(nsnode.namespaces[nil].href == "http://heiser")
58
+ assert(nsnode.namespaces['xmlns'].href == "http://heiser")
59
+ assert(nsnode.namespaces['aaa'].href == "http://heise2")
60
+ assert(nsnode.namespaces['test'].href == "http://heise1")
61
+
62
+ assert(doc.namespaces.length == 5)
63
+ nsnode.namespaces.delete_at('aaa')
64
+ assert(nsnode.namespaces['aaa'].nil?)
65
+
66
+ assert(doc.namespaces.length == 4)
67
+ nsnode.namespaces.delete_all!
68
+ assert(doc.namespaces.length == 2)
69
+ end
70
+ end
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
4
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
5
+
6
+ class TestNamespaceDetailes < MiniTest::Unit::TestCase
7
+ def test_namespace_detailed
8
+ # No closure, so changes are temporary
9
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE.xml")
10
+
11
+ # add an element hallo to root
12
+ nsnode = doc.root.add("hallo")
13
+
14
+ assert(nsnode.dump == "<hallo/>")
15
+
16
+ # add a namespace to the element
17
+ ns = nsnode.namespaces.add("test","http://heise")
18
+
19
+ # has the element a namespace? - No
20
+ assert(!nsnode.namespace?)
21
+
22
+ # set one
23
+ nsnode.namespace = ns
24
+
25
+ # has the element a namespace? - Yes
26
+ assert(nsnode.namespace?)
27
+
28
+ # inspect the namespace + print (to_s)
29
+ assert(ns.prefix == 'test')
30
+ assert(ns.href == 'http://heise')
31
+
32
+ # add a node, a namespace, then add the namespace to the element (via a string)
33
+ nsnode = doc.root.add("hallo")
34
+ nsnode.namespaces["sulu"] = "http://slashdot"
35
+ assert(nsnode.namespaces["soap"].nil?)
36
+ assert(nsnode.namespaces["sulu"].prefix == 'sulu')
37
+
38
+ nsnode.namespace = "sulu"
39
+ assert(nsnode.namespaces["sulu"].prefix == 'sulu')
40
+
41
+ nsnode.namespace = "http://slashdot"
42
+ assert(nsnode.namespaces["sulu"].prefix == 'sulu')
43
+ end
44
+ end
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
4
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
5
+
6
+ class TestNamespaceFind < MiniTest::Unit::TestCase
7
+ def test_namespace_find
8
+ # No closure, so changes are temporary
9
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE.xml")
10
+
11
+ # add a node with namespace
12
+ nsnode = doc.root.add("hallo")
13
+ nsnode.namespaces.add("test","http://heise")
14
+ nsnode.add("test:entry")
15
+
16
+ # find node
17
+ assert(doc.find("//test:entry").empty? == false)
18
+
19
+ doc.register_namespace("t","http://heise")
20
+ assert(nsnode.find("t:entry").empty? == false)
21
+ end
22
+ end
data/test/tc_nested.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require 'time'
4
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
5
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
6
+
7
+ class TestNested < MiniTest::Unit::TestCase
8
+ def test_nested
9
+ nums = 5000
10
+ soc = XML::Smart.string("<?xml version='1.0'?><root><node id='1'><text>I am a text</text></node><node id='2'/></root>")
11
+ # Watch the power
12
+ start_timing "Nested (#{nums} times)"
13
+ 1.upto(nums) { |i|
14
+ # create a XML document and copy the elements for each node to a file
15
+ soc.find("/root/node").each do |ele|
16
+ ts = ele.find('text')
17
+ if ts.any?
18
+ a = ts.first.to_doc
19
+ end
20
+ end
21
+ ts = soc.find("/root/node").first
22
+ ts.add('text','Haller')
23
+ }
24
+ end_timing
25
+ end
26
+ end
data/test/tc_qname.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
4
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
5
+
6
+ class TestQname < MiniTest::Unit::TestCase
7
+ def test_qname
8
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE.xml")
9
+ doc.register_namespace("s","http://schemas.xmlsoap.org/wsdl/soap/")
10
+
11
+ node = doc.root.find("/test/s:hallo").first
12
+ assert(!node.nil?)
13
+ assert(node.qname == "hallo")
14
+ assert(node.qname.name == 'hallo')
15
+ assert(node.qname.prefix == 'soap')
16
+ assert(node.namespace.instance_of? XML::Smart::Dom::Namespace)
17
+ assert(node.qname.to_s == 'soap:hallo')
18
+ assert(node.qname.instance_of? XML::Smart::QName)
19
+
20
+ node = doc.root.find("/test").first
21
+ assert(node.qname.to_s == 'test')
22
+
23
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE-NS.xml")
24
+
25
+ node = doc.root.find("/xmlns0:test").first
26
+ assert(!node.nil?)
27
+ assert(node.qname == "test")
28
+ assert(node.qname.to_s == "test")
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
4
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
5
+
6
+ class TestRelaxng < MiniTest::Unit::TestCase
7
+ def test_relaxng
8
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/HELLO.xml")
9
+ doc.xinclude!
10
+ xsd = XML::Smart.open(::File.dirname(__FILE__) + "/HELLO.rng")
11
+
12
+ nums = 10000
13
+ # Watch the power
14
+ start_timing "RelaxNG #validate_against (#{nums} times)"
15
+ nums.times do
16
+ doc.validate_against(xsd)
17
+ end
18
+ assert(doc.validate_against(xsd) == true)
19
+ end_timing
20
+
21
+ doc.find('/hellos/hello[3]').each do |h|
22
+ h.qname.name = 'test'
23
+ end
24
+ assert(!doc.validate_against(xsd))
25
+
26
+ doc.validate_against(xsd) do |err|
27
+ assert("#{err.file} line #{err.line}: #{err.to_s}" =~ /HELLO.xml line 4: Did not expect element test there/)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require 'time'
4
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
5
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
6
+
7
+ class TestSetOrReplace < MiniTest::Unit::TestCase
8
+ def test_set_or_replace
9
+ nums = 1000
10
+
11
+ start_timing "#replace_by, #root= (#{nums} times)"
12
+ 1.upto(nums) { |i|
13
+ # create a XML document and copy the elements for each node to a file
14
+ soc = XML::Smart.string("<?xml version='1.0'?><root><node id='1'><text>I am a text</text></node><node id='2'/></root>")
15
+ soc.root.find("/root/node").each { |ele|
16
+ cdoc = XML::Smart.open(::File.dirname(__FILE__) + "/#{ele.attributes["id"]}.xml")
17
+ cdoc.root.replace_by(ele)
18
+ }
19
+
20
+ # create a XML document and copy the elements for each node to a file
21
+ soc = XML::Smart.string("<?xml version='1.0'?><root><node id='1'><text>I am a text</text></node><node id='2'/></root>")
22
+ soc.root.find("/root/node").each { |ele|
23
+ cdoc = XML::Smart.open(::File.dirname(__FILE__) + "/#{ele.attributes["id"]}.xml")
24
+ cdoc.root = ele
25
+ }
26
+
27
+ # create a file and copy a newly created XML document to it
28
+ cdoc = XML::Smart.open(::File.dirname(__FILE__) + "/3.xml")
29
+ cdoc.root = XML::Smart.string("<root><node id='1'><text>I am a text</text></node><node id='#{i}'/></root>").root
30
+ }
31
+ end_timing
32
+
33
+ soc = XML::Smart.string("<?xml version='1.0'?><root><node id='1'><text>I am a text</text></node><node id='2'/></root>")
34
+ eles = soc.root.find("/root/node")
35
+
36
+ cdoc = XML::Smart.open(::File.dirname(__FILE__) + "/#{eles[0].attributes["id"]}.xml")
37
+ cdoc.root.replace_by(eles[0])
38
+ assert(cdoc.root.dump == "<node id=\"1\">\n <text>I am a text</text>\n</node>")
39
+
40
+ cdoc = XML::Smart.open(::File.dirname(__FILE__) + "/#{eles[1].attributes["id"]}.xml")
41
+ cdoc.root.replace_by(eles[1])
42
+ assert(cdoc.root.dump == "<node id=\"2\"/>")
43
+
44
+ cdoc = XML::Smart.open(::File.dirname(__FILE__) + "/#{eles[0].attributes["id"]}.xml")
45
+ cdoc.root = eles[0]
46
+ assert(cdoc.root.dump == "<node id=\"1\">\n <text>I am a text</text>\n</node>")
47
+
48
+ cdoc = XML::Smart.open(::File.dirname(__FILE__) + "/#{eles[1].attributes["id"]}.xml")
49
+ cdoc.root = eles[1]
50
+ assert(cdoc.root.dump == "<node id=\"2\"/>")
51
+
52
+ # create a file and copy a newly created XML document to it
53
+ cdoc = XML::Smart.open(::File.dirname(__FILE__) + "/3.xml")
54
+ cdoc.root = XML::Smart.string("<root><node id='1'><text>I am a text</text></node><node id='x'/></root>").root
55
+ cdoc.root.dump == "<root>\n <node id=\"1\">\n <text>I am a text</text>\n </node>\n <node id=\"x\"/>\n</root>"
56
+ end
57
+ end
data/test/tc_sort.rb ADDED
@@ -0,0 +1,18 @@
1
+ # coding: utf-8
2
+ require 'rubygems'
3
+ require 'minitest/autorun'
4
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
5
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
6
+
7
+ class TestSort < MiniTest::Unit::TestCase
8
+ def test_sort
9
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE.xml")
10
+ nodes = doc.find("//names/name[@team>0]").sort{|a,b| a.to_s <=> b.to_s}
11
+
12
+ assert(nodes[0].text == 'Egon')
13
+ assert(nodes[1].text == 'Jürgen')
14
+ assert(nodes[2].text == 'Kathrin ')
15
+ assert(nodes[3].text == 'Michel')
16
+ assert(nodes[4].text == 'Raphi')
17
+ end
18
+ end
data/test/tc_string.rb ADDED
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+ require 'rubygems'
3
+ require 'minitest/autorun'
4
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
5
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
6
+
7
+ class TestString < MiniTest::Unit::TestCase
8
+ def test_String
9
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE.xml")
10
+
11
+ # create a string
12
+ xmldoc = "<?xml version='1.0'?>\n"
13
+ xmldoc << "<contributors>
14
+ <guy id='1' nick='eTM'>Jürgen</guy>
15
+ <guy id='2' nick='chris2'>Christian</guy>
16
+ <guy id='3'>Emanuel</guy>
17
+ <guy id='4'>41</guy>\n"
18
+ xmldoc << "</contributors>"
19
+
20
+ # parse it
21
+ doc = XML::Smart.string(xmldoc)
22
+
23
+ # output all guys with a nickname (the ü is a UTF-8 character in the output)
24
+ # cause (always) everything that is returned is UTF-8
25
+ nodes = doc.root.find("/contributors/guy[@nick]")
26
+ assert("#{nodes[0]} (#{nodes[0].attributes.include?("nick")})" == "Jürgen (true)")
27
+ assert("#{nodes[0]} (#{nodes[0].attributes["nick"]})" == "Jürgen (eTM)")
28
+ assert("#{nodes[1]} (#{nodes[1].attributes.include?("nick")})" == "Christian (true)")
29
+ assert("#{nodes[1]} (#{nodes[1].attributes["nick"]})" == "Christian (chris2)")
30
+ assert(nodes[2].nil?)
31
+
32
+ # query arthur dent
33
+ assert((doc.root.find("/contributors/guy[@id='4']").first.to_i + 1) == 42)
34
+
35
+ # write to a file
36
+ File.unlink(::File.dirname(__FILE__) + "/EXAMPLE.str.xml") rescue nil
37
+ File.open(::File.dirname(__FILE__) + "/EXAMPLE.str.xml",File::WRONLY|File::CREAT|File::TRUNC) { |f| doc.save_as f }
38
+
39
+ assert(XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE.str.xml").root.to_s == XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE.str.xml.test").root.to_s)
40
+ end
41
+ end
data/test/tc_todoc.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
4
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
5
+
6
+ class TestToDoc < MiniTest::Unit::TestCase
7
+ def test_todoc
8
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE-NS.xml")
9
+ doc.root.children[0].add('soap:haller')
10
+ doc.register_namespace :d, 'http://example.org'
11
+
12
+ nums = 10000
13
+
14
+ assert(doc.find("/d:test/d:names").first.to_doc.root.namespaces.length == 2)
15
+ assert(doc.find("/d:test/d:names/d:name[2]").first.to_doc.root.namespaces.length == 1)
16
+ assert(doc.find("/d:test").first.to_doc.root.namespaces.length == 2)
17
+
18
+ # Watch the power
19
+ start_timing "#to_doc (#{nums} times)"
20
+ nums.times do
21
+ doc.find("/d:test/d:names/d:name[2]").first.to_doc
22
+ end
23
+ end_timing
24
+
25
+ assert(doc.find("/d:test/d:names/d:name[2]").first.to_doc.root.dump == '<name xmlns="http://example.org" team="1">J&#xFC;rgen</name>')
26
+ end
27
+ end
data/test/tc_write.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
4
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
5
+
6
+ class TestWrite < MiniTest::Unit::TestCase
7
+ def test_write
8
+ tim = Time.now
9
+
10
+ # When a closure is used, then the changes are written back to the disk
11
+ XML::Smart.modify(::File.dirname(__FILE__) + "/EXAMPLE.xml") do |doc|
12
+ node = doc.root.find("/test/names/name")[0]
13
+ node.text = tim
14
+ end
15
+
16
+ # Print the document
17
+ assert(XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE.xml").find("string(/test/names/name)") == tim.to_s)
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require 'pp'
4
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
5
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
6
+
7
+ class TestXinclude < MiniTest::Unit::TestCase
8
+ def test_xinclude
9
+ nums = 10000
10
+
11
+ # Watch the power
12
+ start_timing "#xinclude! (#{nums} times)"
13
+ nums.times do
14
+ doc = XML::Smart.open_unprotected(::File.dirname(__FILE__) + "/HELLO.xml")
15
+ doc.xinclude!
16
+ end
17
+ end_timing
18
+
19
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/HELLO.xml")
20
+ doc.xinclude!
21
+
22
+ stuff = doc.find('/hellos/*')
23
+ assert(stuff[4].text == "hello me")
24
+ assert(stuff[5].text == "hello curucamp")
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
4
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
5
+
6
+ class TestXmlSchema < MiniTest::Unit::TestCase
7
+ def test_xmlschema
8
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/HELLO.xml")
9
+ doc.xinclude!
10
+ xsd = XML::Smart.open(::File.dirname(__FILE__) + "/HELLO.xsd")
11
+
12
+ nums = 500
13
+ # Watch the power
14
+ start_timing "XML Schema #validate_against (#{nums} times)"
15
+ nums.times do
16
+ doc.validate_against(xsd)
17
+ end
18
+ assert(doc.validate_against(xsd) == true)
19
+ end_timing
20
+
21
+ doc.find('/hellos/hello[3]').each do |h|
22
+ h.qname.name = 'test'
23
+ end
24
+ assert(!doc.validate_against(xsd))
25
+
26
+ doc.validate_against(xsd) do |err|
27
+ assert("#{err.file} line #{err.line}: #{err.to_s}" =~ /HELLO.xml line 4: Did not expect element test there/)
28
+ end
29
+ end
30
+ end
data/test/tc_xpath.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
4
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
5
+
6
+ class TestXpath < MiniTest::Unit::TestCase
7
+ def test_xpath
8
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE-NS.xml")
9
+ doc.register_namespace 'des', 'http://example.org'
10
+ doc.register_namespace 'ex', 'http://example1.org'
11
+
12
+ assert(doc.find('//des:names').length == 1)
13
+ nodesA = doc.root.find("des:names")
14
+ assert(nodesA.length == 1)
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
4
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
5
+
6
+ class TestXpathAttrs < MiniTest::Unit::TestCase
7
+ def test_xpath_attrs
8
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE.xml")
9
+ node = doc.root.find("/test/names/name[2]/@team").first
10
+
11
+ assert(node.qname == "team")
12
+
13
+ # print attribute name and value
14
+ assert(node.qname + "=" + node.value == 'team=1')
15
+ assert(node.qname + "=" + node.to_s == 'team=1')
16
+
17
+ # set the value of the attribute
18
+ node.value = "Value"
19
+ assert(node.value == 'Value')
20
+
21
+ doc.root.find("/test/names/name[2]/@team").first.value = "Hi all"
22
+ assert(doc.root.find("/test/names/name[2]/@team").first.value == 'Hi all')
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
4
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
5
+
6
+ class TestXpathFunctions < MiniTest::Unit::TestCase
7
+ def test_xpath_functions
8
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE.xml")
9
+ assert(doc.root.find("count(/test/names/name)") == 6.0)
10
+ assert(doc.root.find("string(/test/names/name/@team)") == '0')
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
4
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
5
+
6
+ class TestXpathRoot < MiniTest::Unit::TestCase
7
+ def test_xpath_root
8
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/EXAMPLE.xml")
9
+ nodes = doc.find("//*/text()")
10
+ assert(nodes[0].path == "/test/names/name[1]/text()")
11
+ assert(nodes[1].path == "/test/names/name[2]/text()")
12
+ assert(nodes[2].path == "/test/names/name[3]/text()")
13
+ assert(nodes[3].path == "/test/names/name[4]/text()")
14
+ assert(nodes[4].path == "/test/names/name[5]/text()")
15
+ assert(nodes[5].path == "/test/names/name[5]/b/text()")
16
+ assert(nodes[6].path == "/test/names/name[6]/text()")
17
+ assert(nodes[7].path == "/test/names/test_node/text()[1]")
18
+ assert(nodes[8].path == "/test/names/test_node/text()[2]")
19
+ assert(nodes[9].path == "/test/names/test_node/text()[3]")
20
+ assert(nodes[10].path == "/test/colors/blue/text()")
21
+ assert(nodes[11].nil?)
22
+ end
23
+ end
data/test/tc_xsl.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require File.expand_path(::File.dirname(__FILE__) + '/../lib/xml/smart')
4
+ require File.expand_path(::File.dirname(__FILE__) + '/smartrunner.rb')
5
+
6
+ class TestXSL < MiniTest::Unit::TestCase
7
+ def test_xsl
8
+ doc = XML::Smart.open(::File.dirname(__FILE__) + "/XSL_BASE.xml")
9
+ style = XML::Smart.open(::File.dirname(__FILE__) + "/XSL_DOCUMENT.xml")
10
+
11
+ nums = 1000
12
+
13
+ # Watch the power
14
+ start_timing "#transform_with (#{nums} times)"
15
+ nums.times do
16
+ doc.transform_with(style)
17
+ end
18
+ end_timing
19
+
20
+ assert(doc.root.to_s == XML::Smart.open(::File.dirname(__FILE__) + "/XSL_BASE.xml.test").root.to_s)
21
+ end
22
+ end
data/xml-smart.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "xml-smart"
3
+ s.version = "0.3.0"
4
+ s.platform = Gem::Platform::RUBY
5
+ s.summary = "An xml library that doesn't suck - since 2004."
6
+
7
+ s.description = <<-EOF
8
+ Write useful stuff.
9
+
10
+ Also see http://www.wst.univie.ac.at/~mangler/xml-smart/.
11
+ EOF
12
+
13
+ s.files = Dir['{lib/**/*,example/**/*}'] + %w(COPYING Rakefile xml-smart.gemspec README.rdoc AUTHORS)
14
+ s.require_path = 'lib'
15
+ s.extra_rdoc_files = ['README.rdoc']
16
+ s.test_files = Dir['test/tc_*.rb','test/*.xml','test/*.rng','test/*.xsd','test/*.test','test/smartrunner.rb']
17
+
18
+ s.authors = ['Juergen eTM Mangler']
19
+ s.email = 'juergen.mangler@gmail.com'
20
+ s.homepage = 'http://www.wst.univie.ac.at/~mangler/xml-smart/'
21
+
22
+ s.add_runtime_dependency 'nokogiri'
23
+ s.add_runtime_dependency 'lockfile'
24
+ s.add_runtime_dependency 'term-ansicolor'
25
+ s.add_runtime_dependency 'minitest'
26
+ end