September 21, 2005

xml:id went W3C Recommendation

Little xml:id spec finally got W3C Recommendation status. I believe XML programming would be better be xml:id done in 1998, not in 2005. Anyway. xml:id provides a mechanism for annotating elements with unique identifiers. You just set xml:id attribute for an element and you done, no need for DTD, XML ...

I hope Microsoft XML Team would consider adding support for xml:id into the next .NET version and into XLinq. That's really valuable addition to the XML Core.

And of course I cannot avoid Canonical XML (C14N) and xml:id controversy. Read Norm for the crux of the issue. In short - Canonical XML is broken. Here is an illustration:

string xml = @"
<foo xml:id=""f42"" xml:base=""http://foo.com"">
  <bar xml:base=""dir"">baz
</foo>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlDsigC14NTransform c14n = new XmlDsigC14NTransform();
c14n.LoadInput(doc.SelectNodes("/foo/bar"));
StreamReader sr = new StreamReader((Stream)c14n.GetOutput());
Console.WriteLine(sr.ReadToEnd());
Guess what is the result?
<bar xml:base="dir" xml:id="f42"></bar>
Not only xml:id is inherited by bar element (so now you can get different element when searching by the same ID), but xml:base is broken (absolute base URI part is lost). Too bad. Canonical XML should definitely be fixed.