473,396 Members | 1,987 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,396 software developers and data experts.

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

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:output_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
Aug 25 '08 #1
2 4947
On 25 Aug, 11:43, KLEIN Stéphane <steph...@is-webdesign.comwrote:
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-Cor...ml#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
Aug 25 '08 #2
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
Aug 25 '08 #3

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

Similar topics

0
by: Skip Montanaro | last post by:
I downloaded and built Object Craft's Sybase module but can't import it: % PYTHONSTARTUP= python Python 2.4a0 (#4, Mar 31 2004, 15:02:21) on sunos5 Type "help", "copyright", "credits" or...
3
by: Paramesh | last post by:
Hello friends, My friend asked me this question: This question regards proprietary software (of which I am one of the developers), so I cannot post actual code for this question. I will try...
1
by: zxo102 | last post by:
Hi everyone, I need your help for using python odbc module. I have a simple table defined as create table A ( userId char(10), courseid char(10), grade integer, primary key(userId,courseId)...
3
by: Jia Lu | last post by:
Hi all I wonder if I can import a file with other file extensions ? Can I do that only with python? Thank you Jia Lu
1
by: Jia Lu | last post by:
Hi all I tried to scan a directory and __import__ all modules , <log> imported module: help imported module: __init__ imported module: hi imported module: thanks and I scaned all methods in...
1
by: Sonu | last post by:
hello, i need to create script that can create a csv file as i want for example i have two table in database that are person ,country if i want to create csv file to person,then wht to do ,, how...
2
by: smalltalk | last post by:
in arcgis , arcgisscripting is a dll file there is a script as following: import arcgisscripting gp = arcgisscripting.create() why can the script run? how can we setup a dll file as a...
0
by: prudhivi | last post by:
hi, i have a database about parameters in a C file. I want to open that C file and access thae Parameter values in a Perl module. can you please suggest a solution for this. Thanks prudhivi
1
by: moepusu | last post by:
Hi How can I write a command to browse the file folder in Module? Thank you in advance
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.