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

Discussion in 'Python' started by KLEIN Stéphane, Aug 25, 2008.

  1. 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
     
    KLEIN Stéphane, Aug 25, 2008
    #1
    1. Advertising

  2. KLEIN Stéphane

    Paul Boddie Guest

    On 25 Aug, 11:43, KLEIN Stéphane <> 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:

    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
     
    Paul Boddie, Aug 25, 2008
    #2
    1. Advertising

  3. 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
     
    KLEIN Stéphane, Aug 25, 2008
    #3
  4. KLEIN Stéphane

    Paul Boddie Guest

    On 25 Aug, 14:56, KLEIN Stéphane <> wrote:
    >


    [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
     
    Paul Boddie, Aug 25, 2008
    #4
    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. Replies:
    0
    Views:
    1,567
  2. Arne Nordmann
    Replies:
    0
    Views:
    542
    Arne Nordmann
    Jun 28, 2004
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,458
    Dimitre Novatchev
    Aug 24, 2006
  4. Replies:
    3
    Views:
    548
    Stefan Behnel
    Aug 3, 2007
  5. Replies:
    11
    Views:
    215
Loading...

Share This Page