1.1 --- a/tests/namespaces.py Sun Dec 11 20:48:34 2005 +0000
1.2 +++ b/tests/namespaces.py Sun Dec 11 20:48:47 2005 +0000
1.3 @@ -2,6 +2,9 @@
1.4
1.5 import libxml2dom, xml.dom.minidom
1.6
1.7 +print
1.8 +print "This is libxml2dom's behaviour for default namespaces:"
1.9 +print
1.10 document = libxml2dom.createDocument(None, "doc", None)
1.11 top = document.xpath("*")[0]
1.12 elem1 = document.createElementNS("DAV:", "href")
1.13 @@ -10,12 +13,26 @@
1.14 elem2 = document.createElementNS(None, "no_ns")
1.15 print "Namespace is", repr(elem2.namespaceURI)
1.16 document.xpath("*")[0].appendChild(elem2)
1.17 +print "Find href", len(document.xpath("href")) != 0
1.18 +print "Find x:href", len(document.xpath("x:href", namespaces={"x": "DAV:"})) != 0
1.19 +print "Find //no_ns", len(document.xpath("//no_ns")) != 0
1.20 +print "Find x:href/no_ns", len(document.xpath("x:href/no_ns", namespaces={"x": "DAV:"})) != 0
1.21 print document.toString()
1.22 document.toFile(open("test_ns.xml", "wb"))
1.23
1.24 document = libxml2dom.parse("test_ns.xml")
1.25 print "Namespace is", repr(document.xpath("*")[0].namespaceURI)
1.26 +print "Namespace is", repr(document.xpath("*/*")[0].namespaceURI)
1.27 +print "Find href", len(document.xpath("href")) != 0
1.28 +print "Find x:href", len(document.xpath("x:href", namespaces={"x": "DAV:"})) != 0
1.29 +print "Find //no_ns", len(document.xpath("//no_ns")) != 0
1.30 +print "Find x:href/no_ns", len(document.xpath("x:href/no_ns", namespaces={"x": "DAV:"})) != 0
1.31 +print document.toString()
1.32 +print "--------"
1.33
1.34 +print
1.35 +print "This is minidom's behaviour for default namespaces:"
1.36 +print
1.37 document = xml.dom.minidom.Document()
1.38 elem1 = document.createElementNS("DAV:", "href")
1.39 print "Namespace is", repr(elem1.namespaceURI)
1.40 @@ -28,14 +45,29 @@
1.41
1.42 document = xml.dom.minidom.parse("test_ns.xml")
1.43 print "Namespace is", repr(document.documentElement.namespaceURI)
1.44 +print "Namespace is", repr([n for n in document.documentElement.childNodes if n.nodeType == n.ELEMENT_NODE][0].namespaceURI)
1.45 +print document.toxml()
1.46 +print "--------"
1.47
1.48 try:
1.49 from xml.dom.ext import PrettyPrint
1.50 + print
1.51 + print "This is minidom's behaviour for default namespaces with PrettyPrint from PyXML:"
1.52 + print
1.53 + document = xml.dom.minidom.Document()
1.54 + elem1 = document.createElementNS("DAV:", "href")
1.55 + print "Namespace is", repr(elem1.namespaceURI)
1.56 + document.appendChild(elem1)
1.57 + elem2 = document.createElementNS(None, "no_ns")
1.58 + print "Namespace is", repr(elem2.namespaceURI)
1.59 + document.childNodes[0].appendChild(elem2)
1.60 PrettyPrint(document)
1.61 PrettyPrint(document, stream=open("test_ns.xml", "wb"))
1.62
1.63 document = xml.dom.minidom.parse("test_ns.xml")
1.64 print "Namespace is", repr(document.documentElement.namespaceURI)
1.65 + print "Namespace is", repr([n for n in document.documentElement.childNodes if n.nodeType == n.ELEMENT_NODE][0].namespaceURI)
1.66 + PrettyPrint(document)
1.67
1.68 except ImportError:
1.69 print "Prettyprinted document not produced."