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

Discussion in 'Python' started by Greg Wogan-Browne, Jan 25, 2005.

  1. 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')


    >>>
    Greg Wogan-Browne, Jan 25, 2005
    #1
    1. Advertising

  2. Greg Wogan-Browne

    Uche Ogbuji Guest

    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/edu/x-dw-x-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/developerworks/xml/library/x-think28.html
    Manage XML collections with XAPI -
    http://www-106.ibm.com/developerworks/xml/library/x-xapi.html
    Default and error handling in XSLT lookup tables -
    http://www.ibm.com/developerworks/xml/library/x-tiplook.html
    Packaging XSLT lookup tables as EXSLT functions -
    http://www.ibm.com/developerworks/xml/library/x-tiplook2.html
    Uche Ogbuji, Jan 28, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike McGavin
    Replies:
    5
    Views:
    701
    Mike McGavin
    Jan 19, 2005
  2. Sakcee
    Replies:
    1
    Views:
    303
  3. Replies:
    3
    Views:
    517
    Stefan Behnel
    Aug 3, 2007
  4. Replies:
    6
    Views:
    1,937
  5. ming
    Replies:
    2
    Views:
    122
Loading...

Share This Page