Your own ID attributes as link destinations

by Bob DuCharme


The XML world has debated the best way to assign identity to elements; choices include attributes declared as being of type ID in a DTD or schema, the recently updated xml:id proposal, and rdf:ID attributes. I recently discovered that when you send Mozilla or IE to an XML document that points to a stylesheet transforming it to HTML, and that transformation adds unique ID values to the HTML versions of the elements, a fragment identifier of a particular element's ID added to the URL sends the browser right to that element. You can base these ID values on attribute (or element!) values from your original data, which means that any values that you use to assign identity to elements can turn those elements into Mozilla or IE linking destinations.



For example, this document lists inventory elements using element and attribute names that I just made up. If you do a View Source on the document, you'll see that it's an XML document with no HTML elements, and that an XSLT stylesheet converts it to HTML when your browser pulls it down. The light blue quick-drying potrzebie part element has a partNum attribute value of p3141. Now try this link: http://www.snee.com/xml/inven.xml#p3141. It goes right to the light blue quick-drying potrzebie because the stylesheet that the inven.xml document points to converts part elements to h2 elements with each part element's partNum attribute value used to create an id attribute for the p element. In other words, you're linking to a specific part element by using its partNum value.



I've written before ("Goodbye to the A element's NAME attribute") about how Mozilla and IE no longer require a name attribute on a elements to let you link to a point within those web pages, because an id attribute on any HTML element can now turn it into a link destination. Some HTML tricks don't always work with the HTML created in the browser by a client-side XSLT transformation, and the fact that this ID transformation trick does work means that we all have a little more flexibility with our data design and our linking.





What's your favorite way to assign identity to XML elements?