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