473,715 Members | 6,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="ht tp://www.w3.org/1999/xlink"
xmlns:sodipodi= "http://sodipodi.source forge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape= "http://www.inkscape.or g/namespaces/inkscape"
width="210mm"
height="297mm"
id="svg2383"
sodipodi:versio n="0.32"
inkscape:versio n="0.46"
sodipodi:docnam e="product_page .svg"
inkscape:output _extension="org .inkscape.outpu t.svg.inkscape" >
</svg:svg>
"""

$ ipython

In [1]: from xml.dom import minidom

In [2]: dom1 = minidom.parse(" myfile.svg")

In [3]: print(dom1.getE lementById(u"sv g2383"))
None

In [4]: print(dom1.getE lementById("svg 2383"))
None

I don't understand why getElementById return always None.

Other example with libxml2dom library :

$ ipython

In [1]: import libxml2dom

In [2]: dom2 = libxml2dom.pars eFile("myfile.s vg")

In [3]: print(dom2.getE lementById(u"sv g2383"))
None

In [4]: print(dom2.getE lementById("svg 2383"))

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 4970
On 25 Aug, 11:43, KLEIN Stéphane <steph...@is-webdesign.comwr ote:
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.getE lementById(u"sv g2383"))
None

In [4]: print(dom1.getE lementById("svg 2383"))
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.pars eFile("myfile.s vg")

In [3]: print(dom2.getE lementById(u"sv g2383"))
None

In [4]: print(dom2.getE lementById("svg 2383"))

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
1507
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 "license" for more information. >>> import Sybase Traceback (most recent call last): File "<stdin>", line 1, in ? File "/home/titan/skipm/local/lib/python2.4/site-packages/Sybase.py", line 20, in ?
3
1583
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 to use illustrative examples. I have two libraries for distribution; one is an essential library and the other is an add-on (providing specialized extensions and
1
2493
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
2092
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
3490
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 them, and put them to a list like:
1
1398
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 to connect database,how to create csv of interrelated field eg :in person table :country_id (which is primary key of country table)
2
1730
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 module?
0
1182
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
1411
by: moepusu | last post by:
Hi How can I write a command to browse the file folder in Module? Thank you in advance
0
8823
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9104
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2119
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.