paulb@90 | 1 | #!/usr/bin/env python |
paulb@90 | 2 | |
paulb@90 | 3 | "Test of elements and attribute interfaces." |
paulb@90 | 4 | |
paulb@90 | 5 | import libxml2dom |
paulb@90 | 6 | |
paulb@90 | 7 | d = libxml2dom.parseString("<ddd:doc xmlns:ddd='http://ddd'/>") |
paulb@90 | 8 | e = d.xpath("*")[0] |
paulb@90 | 9 | assert e.parentNode == d |
paulb@90 | 10 | assert e.namespaceURI == "http://ddd" |
paulb@90 | 11 | assert e.nodeName == e.tagName == "ddd:doc" |
paulb@90 | 12 | assert e.localName == "doc" |
paulb@93 | 13 | print d.toString() |
paulb@93 | 14 | |
paulb@90 | 15 | e.setAttributeNS("http://xxx", "xxx:x", "y") |
paulb@90 | 16 | assert e.getAttributeNS("http://xxx", "x") == "y" |
paulb@90 | 17 | l = e.xpath("@*") |
paulb@90 | 18 | assert len(l) == 1 |
paulb@90 | 19 | a = l[0] |
paulb@90 | 20 | assert a.parentNode == e |
paulb@90 | 21 | assert a.namespaceURI == "http://xxx" |
paulb@90 | 22 | assert a.nodeName == "xxx:x" |
paulb@90 | 23 | assert a.localName == "x" |
paulb@90 | 24 | assert a.nodeValue == a.value == "y" |
paulb@93 | 25 | print d.toString() |
paulb@93 | 26 | |
paulb@90 | 27 | a2 = d.createAttributeNS("http://aaa", "aaa:a") |
paulb@90 | 28 | a2.nodeValue = "b" |
paulb@90 | 29 | assert a2.namespaceURI == "http://aaa" |
paulb@90 | 30 | assert a2.nodeName == "aaa:a" |
paulb@90 | 31 | assert a2.localName == "a" |
paulb@90 | 32 | assert a2.nodeValue == a2.value == "b" |
paulb@93 | 33 | print d.toString() |
paulb@93 | 34 | |
paulb@90 | 35 | e.setAttributeNodeNS(a2) |
paulb@90 | 36 | l2 = e.xpath("@*") |
paulb@90 | 37 | assert len(l2) == 2 |
paulb@93 | 38 | print d.toString() |
paulb@93 | 39 | |
paulb@90 | 40 | e.setAttributeNS("http://ccc", "ccc:c", "d") |
paulb@90 | 41 | assert e.getAttributeNS("http://ccc", "c") == "d" |
paulb@90 | 42 | l3 = e.xpath("@*") |
paulb@90 | 43 | assert len(l3) == 3 |
paulb@90 | 44 | e.setAttribute("p", "q") |
paulb@90 | 45 | assert e.getAttribute("p") == "q" |
paulb@90 | 46 | l4 = e.xpath("@*") |
paulb@90 | 47 | assert len(l4) == 4 |
paulb@93 | 48 | al = e.attributes |
paulb@93 | 49 | assert len(al.items()) == 4 |
paulb@93 | 50 | a3 = al.getNamedItemNS("http://ccc", "c") |
paulb@93 | 51 | assert a3.namespaceURI == "http://ccc" |
paulb@93 | 52 | assert a3.nodeName == "ccc:c" |
paulb@93 | 53 | assert a3.localName == "c" |
paulb@93 | 54 | assert a3.nodeValue == "d" |
paulb@93 | 55 | print d.toString() |
paulb@93 | 56 | |
paulb@93 | 57 | a4 = e.createAttribute("m") |
paulb@93 | 58 | a4.nodeValue = "n" |
paulb@93 | 59 | assert a4.namespaceURI == None |
paulb@93 | 60 | assert a4.nodeName == "m" |
paulb@93 | 61 | assert a4.localName == "m" |
paulb@93 | 62 | assert a4.nodeValue == a4.value == "n" |
paulb@93 | 63 | print d.toString() |
paulb@93 | 64 | |
paulb@93 | 65 | a4_old = al.setNamedItem(a4) |
paulb@93 | 66 | assert a4_old == None |
paulb@93 | 67 | assert len(al.items()) == 5 |
paulb@93 | 68 | assert e.getAttribute("m") == "n" |
paulb@93 | 69 | al.removeNamedItem("m") |
paulb@93 | 70 | assert not e.hasAttribute("m") |
paulb@93 | 71 | assert len(al.items()) == 4 |
paulb@93 | 72 | e.removeAttributeNS("http://ccc", "c") |
paulb@93 | 73 | assert not e.hasAttributeNS("http://ccc", "c") |
paulb@93 | 74 | l5 = e.xpath("@*") |
paulb@93 | 75 | assert len(l5) == 3 |
paulb@93 | 76 | e.removeAttribute("p") |
paulb@93 | 77 | assert not e.hasAttribute("p") |
paulb@93 | 78 | l6 = e.xpath("@*") |
paulb@93 | 79 | assert len(l6) == 2 |
paulb@90 | 80 | print d.toString() |
paulb@90 | 81 | |
paulb@90 | 82 | # vim: tabstop=4 expandtab shiftwidth=4 |