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

K

KLEIN Stéphane

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.

This is my example :

My SVG file :

"""
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg:svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="210mm"
height="297mm"
id="svg2383"
sodipodi:version="0.32"
inkscape:version="0.46"
sodipodi:docname="product_page.svg"
inkscape:eek:utput_extension="org.inkscape.output.svg.inkscape">
</svg:svg>
"""

$ ipython

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.

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.

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

Thanks for your informations.
Stephane
 
P

Paul Boddie

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:

http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-getElBId

"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.

Paul

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:

https://hg.boddie.org.uk/libxml2dom
 
K

KLEIN Stéphane

Le Mon, 25 Aug 2008 04:30:00 -0700, Paul Boddie a écrit :
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.

Thanks ! It work.

Regards,
Stephane
 
P

Paul Boddie

[DOM implementations, identifiers and validation]
Thanks ! It work.

I've since updated libxml2dom to support validation and provide a more
reliable getElementById method. Unfortunately, I can't seem to make
the example document validate against any of the official SVG DTDs,
and yet it seems necessary to ask libxml2 to perform validation in
order to make getElementById return desirable results (in the presence
of mere references to the DTDs rather than inlining the huge DTDs
themselves).

Interestingly, the example from the Wikipedia page on SVG, with some
id attributes added, appears to work just fine:

http://en.wikipedia.org/wiki/Scalable_Vector_Graphics

Anyway, if this is interesting, please refer to the repository I
mentioned earlier - a release will eventually be made incorporating
these improvements.

Paul
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top