1 #!/usr/bin/env python 2 3 "Test of elements and attribute interfaces." 4 5 import libxml2macro as n_ 6 7 try: 8 do_not_have_libxml2macro = n_ 9 import libxml2dom 10 use_libxml2dom = 1 11 except NameError: 12 use_libxml2dom = 0 13 14 if __name__ == "__main__": 15 import sys 16 17 s = "<ddd:doc xmlns:ddd='http://ddd'/>" 18 19 if use_libxml2dom: 20 n_d = libxml2dom.parseString(s) 21 else: 22 n_d = parseString(s) 23 24 n_e = n_d.xpath("*")[0] 25 assert n_e.parentNode == n_d 26 assert n_e.namespaceURI == "http://ddd" 27 assert n_e.nodeName == n_e.tagName == "ddd:doc" 28 assert n_e.localName == "doc" 29 print n_d.toString() 30 31 n_e.setAttributeNS("http://xxx", "xxx:x", "y") 32 assert n_e.getAttributeNS("http://xxx", "x") == "y" 33 l = n_e.xpath("@*") 34 assert len(l) == 1 35 n_a = l[0] 36 assert n_a.parentNode == n_e 37 assert n_a.namespaceURI == "http://xxx" 38 assert n_a.nodeName == "xxx:x" 39 assert n_a.localName == "x" 40 assert n_a.nodeValue == n_a.value == "y" 41 print n_d.toString() 42 43 n_a2 = n_d.createAttributeNS("http://aaa", "aaa:a") 44 n_a2.nodeValue = "b" 45 assert n_a2.namespaceURI == "http://aaa" 46 assert n_a2.nodeName == "aaa:a" 47 assert n_a2.localName == "a" 48 assert n_a2.nodeValue == n_a2.value == "b" 49 print n_d.toString() 50 51 n_e.setAttributeNodeNS(n_a2) 52 l2 = n_e.xpath("@*") 53 assert len(l2) == 2 54 print n_d.toString() 55 56 n_e.setAttributeNS("http://ccc", "ccc:c", "d") 57 assert n_e.getAttributeNS("http://ccc", "c") == "d" 58 l3 = n_e.xpath("@*") 59 assert len(l3) == 3 60 n_e.setAttribute("p", "q") 61 assert n_e.getAttribute("p") == "q" 62 l4 = n_e.xpath("@*") 63 assert len(l4) == 4 64 al = n_e.attributes 65 assert len(al.items()) == 4 66 67 if use_libxml2dom: 68 n_a3 = al.getNamedItemNS("http://ccc", "c") 69 assert n_a3.namespaceURI == "http://ccc" 70 assert n_a3.nodeName == "ccc:c" 71 assert n_a3.localName == "c" 72 assert n_a3.nodeValue == "d" 73 print n_d.toString() 74 75 n_a4 = n_e.createAttribute("m") 76 n_a4.nodeValue = "n" 77 assert n_a4.namespaceURI == None 78 assert n_a4.nodeName == "m" 79 assert n_a4.localName == "m" 80 assert n_a4.nodeValue == n_a4.value == "n" 81 print n_d.toString() 82 83 if use_libxml2dom: 84 n_a4_old = al.setNamedItem(n_a4) 85 assert n_a4_old == None 86 assert len(al.items()) == 5 87 assert n_e.getAttribute("m") == "n" 88 al.removeNamedItem("m") 89 assert not n_e.hasAttribute("m") 90 assert len(al.items()) == 4 91 92 n_e.removeAttributeNS("http://ccc", "c") 93 assert not n_e.hasAttributeNS("http://ccc", "c") 94 l5 = n_e.xpath("@*") 95 assert len(l5) == 3 96 n_e.removeAttribute("p") 97 assert not n_e.hasAttribute("p") 98 l6 = n_e.xpath("@*") 99 assert len(l6) == 2 100 print n_d.toString() 101 102 # vim: tabstop=4 expandtab shiftwidth=4