1 #!/usr/bin/env python 2 3 import libxml2dom, xml.dom.minidom 4 5 print 6 print "This is libxml2dom's behaviour for default namespaces:" 7 print 8 document = libxml2dom.createDocument(None, "doc", None) 9 top = document.xpath("*")[0] 10 elem1 = document.createElementNS("DAV:", "href") 11 print "Namespace is", repr(elem1.namespaceURI) 12 document.replaceChild(elem1, top) 13 elem2 = document.createElementNS(None, "no_ns") 14 print "Namespace is", repr(elem2.namespaceURI) 15 document.xpath("*")[0].appendChild(elem2) 16 print "Find href", len(document.xpath("href")) != 0 17 print "Find x:href", len(document.xpath("x:href", namespaces={"x": "DAV:"})) != 0 18 print "Find //no_ns", len(document.xpath("//no_ns")) != 0 19 print "Find x:href/no_ns", len(document.xpath("x:href/no_ns", namespaces={"x": "DAV:"})) != 0 20 print document.toString() 21 document.toFile(open("test_ns.xml", "wb")) 22 23 document = libxml2dom.parse("test_ns.xml") 24 print "Namespace is", repr(document.xpath("*")[0].namespaceURI) 25 print "Namespace is", repr(document.xpath("*/*")[0].namespaceURI) 26 print "Find href", len(document.xpath("href")) != 0 27 print "Find x:href", len(document.xpath("x:href", namespaces={"x": "DAV:"})) != 0 28 print "Find //no_ns", len(document.xpath("//no_ns")) != 0 29 print "Find x:href/no_ns", len(document.xpath("x:href/no_ns", namespaces={"x": "DAV:"})) != 0 30 print document.toString() 31 print "--------" 32 33 print 34 print "This is minidom's behaviour for default namespaces:" 35 print 36 document = xml.dom.minidom.Document() 37 elem1 = document.createElementNS("DAV:", "href") 38 print "Namespace is", repr(elem1.namespaceURI) 39 document.appendChild(elem1) 40 elem2 = document.createElementNS(None, "no_ns") 41 print "Namespace is", repr(elem2.namespaceURI) 42 document.childNodes[0].appendChild(elem2) 43 print document.toxml() 44 open("test_ns.xml", "wb").write(document.toxml()) 45 46 document = xml.dom.minidom.parse("test_ns.xml") 47 print "Namespace is", repr(document.documentElement.namespaceURI) 48 print "Namespace is", repr([n for n in document.documentElement.childNodes if n.nodeType == n.ELEMENT_NODE][0].namespaceURI) 49 print document.toxml() 50 print "--------" 51 52 try: 53 from xml.dom.ext import PrettyPrint 54 print 55 print "This is minidom's behaviour for default namespaces with PrettyPrint from PyXML:" 56 print 57 document = xml.dom.minidom.Document() 58 elem1 = document.createElementNS("DAV:", "href") 59 print "Namespace is", repr(elem1.namespaceURI) 60 document.appendChild(elem1) 61 elem2 = document.createElementNS(None, "no_ns") 62 print "Namespace is", repr(elem2.namespaceURI) 63 document.childNodes[0].appendChild(elem2) 64 PrettyPrint(document) 65 PrettyPrint(document, stream=open("test_ns.xml", "wb")) 66 67 document = xml.dom.minidom.parse("test_ns.xml") 68 print "Namespace is", repr(document.documentElement.namespaceURI) 69 print "Namespace is", repr([n for n in document.documentElement.childNodes if n.nodeType == n.ELEMENT_NODE][0].namespaceURI) 70 PrettyPrint(document) 71 72 except ImportError: 73 print "Prettyprinted document not produced." 74 75 # vim: tabstop=4 expandtab shiftwidth=4