473,378 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Clarification on XML parsing & namespaces (xml.dom.minidom)

Hi all,

I am having some trouble figuring out what is going on here - is this a
bug, or correct behaviour? Basically, when I create an XML document with
a namespace using xml.dom.minidom.parse() or parseString(), the
namespace exists as an xmlns attribute in the DOM (fair enough, as it's
in the original source document). However, if I use the DOM
implementation to create an identical document with a namespace, the
xmlns attribute is not present.

This mainly affects me when I go to print out the document again using
Document.toxml(), as the xmlns attribute is not printed for documents I
create dynamically, and therefore XSLT does not kick in (I'm using an
external processor).

Any thoughts on this would be appreciated. Should I file a bug on pyxml?

Greg
Python 2.3.3 (#1, May 7 2004, 10:31:40)
[GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import xml.dom.minidom
raw = '<test xmlns="http://example.com/namespace"/>'
doc = xml.dom.minidom.parseString(raw)
print doc.documentElement.namespaceURI http://example.com/namespace print doc.documentElement.getAttribute('xmlns') http://example.com/namespace impl = xml.dom.minidom.getDOMImplementation()
doc2 = impl.createDocument('http://example.com/namespace','test',None)
print doc2.documentElement.namespaceURI http://example.com/namespace print doc2.documentElement.getAttribute('xmlns')

Jul 18 '05 #1
1 2741
Greg Wogan-Browne wrote:
I am having some trouble figuring out what is going on here - is this a bug, or correct behaviour? Basically, when I create an XML document with a namespace using xml.dom.minidom.parse() or parseString(), the
namespace exists as an xmlns attribute in the DOM (fair enough, as it's in the original source document). However, if I use the DOM
implementation to create an identical document with a namespace, the
xmlns attribute is not present.

This mainly affects me when I go to print out the document again using Document.toxml(), as the xmlns attribute is not printed for documents I create dynamically, and therefore XSLT does not kick in (I'm using an external processor).

Any thoughts on this would be appreciated. Should I file a bug on

pyxml?

It's odd behavior, but I think it's a stretch to call it a bug. You
problem is that you're mixing namespaced documents with the
non-namespace DOM API. That means trouble and such odd quirks every
time.

Use getAttributeNS, createElementNS, setAttributeNS, etc. rather than
getAttribute, createElement, setAttribute, etc.

--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://4Suite.org http://fourthought.com
Use CSS to display XML -
http://www.ibm.com/developerworks/ed...-xmlcss-i.html
Introducing the Amara XML Toolkit -
http://www.xml.com/pub/a/2005/01/19/amara.html
Be humble, not imperial (in design) -
http://www.adtmag.com/article.asp?id=10286UBL 1.0 -
http://www-106.ibm.com/developerwork...x-think28.html
Manage XML collections with XAPI -
http://www-106.ibm.com/developerwork...ry/x-xapi.html
Default and error handling in XSLT lookup tables -
http://www.ibm.com/developerworks/xm...x-tiplook.html
Packaging XSLT lookup tables as EXSLT functions -
http://www.ibm.com/developerworks/xm...-tiplook2.html

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Alex Mizrahi | last post by:
Hello, All! i have 3mb long XML document with about 150000 lines (i think it has about 200000 elements there) which i want to parse to DOM to work with. first i thought there will be no...
5
by: Mike McGavin | last post by:
Hi everyone. I've been trying for several hours now to get minidom to parse namespaces properly from my stream of XML, so that I can use DOM methods such as getElementsByTagNameNS(). For some...
18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
2
by: Piet | last post by:
Hello, Via Xpath, I want to access nodes which have a namespace prefix. THe document at hand is an Xsl-FO document. I tried the following: from xml.dom import minidom from xml.xpath import...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
36
by: Wilfredo Sánchez Vega | last post by:
I'm having some issues around namespace handling with XML: >>> document = xml.dom.minidom.Document() >>> element = document.createElementNS("DAV:", "href") >>> document.appendChild(element)...
1
by: Paul Kozik | last post by:
I am trying to write a script that reads an XML file (using the minidom module), makes an edit to a few attributes, then saves it back. If I use minidom.Document() to create the xml file, then...
1
by: phasma | last post by:
Hi. I'm trying to disassemble bzipped file. If I use minidom.parseString, I'm getting this error: Traceback (most recent call last): File "./replications.py", line 342, in ? File...
2
by: ashmir.d | last post by:
Hi, I am trying to parse an xml file using the minidom parser. <code> from xml.dom import minidom xmlfilename = "sample.xml" xmldoc = minidom.parse(xmlfilename) </code> The parser is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.