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.