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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?iso-8859-1?q?KLEIN_St=E9phane?=

    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


  • Paul Boddie

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

    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:



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


    Comment

    • =?iso-8859-1?q?KLEIN_St=E9phane?=

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

      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


      Comment

      Working...