I can't fetch dom node in svg file with getElementById method (module minidom and libxml2dom)

Paul Boddie paul at boddie.org.uk
Mon Aug 25 13:30:00 CEST 2008

On 25 Aug, 11:43, KLEIN Stéphane <steph... at is-webdesign.com> wrote:
> Hi,
> I've a xml svg file and I would like to update it with Python.
> First, I would like to fetch one dom node with getElementByID. I've one
> issue about this method.

[SVG file with id attribute on svg element]

> In [1]: from xml.dom import minidom
> In [2]: dom1 = minidom.parse("myfile.svg")
> In [3]: print(dom1.getElementById(u"svg2383"))
> None
> In [4]: print(dom1.getElementById("svg2383"))
> None
> I don't understand why getElementById return always None.

Here's a possible explanation:


"Attributes with the name "ID" are not of type ID unless so defined.
Implementations that do not know whether attributes are of type ID or
not are expected to return null."

> Other example with libxml2dom library :
> $ ipython
> In [1]: import libxml2dom
> In [2]: dom2 = libxml2dom.parseFile("myfile.svg")
> In [3]: print(dom2.getElementById(u"svg2383"))
> None
> In [4]: print(dom2.getElementById("svg2383"))
> I don't understand why getElementById return always None.

Here it's because I programmed it to do so. ;-) In fact, the
getElementById method provided by documents parsed by the
libxml2dom.svg module also return None in this case, although if I
were to take a look at the SVG DTD or schema, perhaps I should provide
such behaviour for SVG documents specifically.

> Well, my final purpose isn't to fetch root dom node but to fetch many
> other sub node.

You could always try using an XPath expression:

  node = (dom2.xpath("//*[@id='svg2383']") or [None])[0]

Similar things could be done in PyXML and other libraries, I'm sure,
but minidom lacks XPath support, if I remember correctly.


P.S. There's so much I could be doing with libxml2dom, but we don't
all have enough time for everything we'd like to do (as I'm sure many
can understand). However, a Mercurial repository tracking the latest
work is available here:


More information about the Python-list mailing list