Wednesday, 2 August 2006

Who knows what a node ID is?

The question of accessing a node in the source tree by it's identity depends upon which SVG viewer you are using.

Up until 9th September 2005 you could rely upon two things:

1) If there was a DTD available, then your parser would identify id attributes as being of type ID if they were declared so in the DTD.

2) Your application assumed that when you use 'id' as an attribute name then it most probably was.

Either way you could use document.getElementById(id) to locate the uniquely identified node in the document tree.

Now with respect to SVG viewers, both Batik and ASV3 make an assumption about nodes in the source tree as being SVGElement nodes. As a result you can use document.getElementById(id) to locate metadata nodes that are not in the SVG namespace.

But, if they are not in the SVG namespace and not declared in the SVG DTD then they should not be accessible by this method. The two new implementations of SVG, Firefox 1.5 and Opera 9 have made this distinction. Neither of these browsers will allow you to use the DOM to retrieve nodes by their ID if they are not SVG elements.

However, there is light at the end of the tunnel. From 9th September 2005 onwards there was a new W3C recommendation published that covered this exact problem. The xml:id recommendation identifies an new XML attribute, like xml:lang, that has a special meaning. If you use an xml:id attribute, the application processing your XML should interpret this as a unique identifier for the owner element regardless of the presence of a DTD or schema and I'm happy to say that Opera 9 supports xml:id but unfortunately, at this moment in time, Firefox 1.5 does not.

So, once again I find myself being bruised and bumped by web standards support.

4 comments:

  1. Very useful weblog, keep up the good work.
    For writing something simple that is cross-viewer, multi-namespaced, valid SVG i find myself having to read all the blogs, not exactly superproductive. Therefore you might want to look into one of the things i started to more actively assist coding, and maybe help improving it: http://wiki.svg.org/Starter

    ReplyDelete
  2. you could use DOM level3 XPath's evaluate mathod, which is implemented in Opera and Firefox.
    see:
    Using the Mozilla Javascript Interface to XPath

    cheers
    Holger

    ReplyDelete
  3. Are you saying i should use both xml:id and id on the same element to make it work in different viewers ?

    ReplyDelete
  4. Doug route, 'you're saying that elements that are in the SVG namespace can use just id instead of xml:id'.

    I don't think that was what I was saying. I was pointing-out the differences between some of the older SVG implementation, that make assumptions just like web browsers used to (and still do) where as the newer ones are, how shall I put it... more precise about the object model and therefore what you actually need to do to get the same results.

    ReplyDelete