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