Kirk Evans is a Microsoft Architect for the Azure Center of Excellence.
Introduction to SharePoint and Azure IaaS
Building SharePoint Apps with Windows Azure Platform as a Service
SharePoint Solutions and Architectures on Windows Azure Infrastructure Services
Understanding Authentication and Permissions with Apps for SharePoint and Office
On the GotDotNet.com message boards, GeFIN asks how to create a unique ID using XML.
There are several ways to accomplish this. For instance, you could use the classes in the System.Xml namespace to load the document, select the nodes from the document, sort them, then get the value of the last node in the node list. You might simply generate a Guid for each node and use that new value. You could retrieve a value from a database, using something like Oracle's sequences, or you could use SQL Server's XML capabilities to shred the document and handle the autoincrement the IDs internally.
Let's look at some ways that we can leverage XSLT to accomplish this task.
The first (and likely simplest) means of generating IDs is to use XSLT's generate-id() function to append arbitrary unique IDs to each node. We use the XSLT identity transformation:
Given the instance document:
The output result is:
In the above example, we generated unique IDs for each node. Further runs of this transformation might provide different IDs for each node, which might not be acceptable. In GeFIN's case, he actually desired the ability to use some type of an autoincrement. Using an autoincrement naturally assumes knowledge of the last maximum value used. While XSLT does not provide a means to find a maximum value, this problem has already been addressed in several ways. EXSLT provides a max function to get a max value from a nodeset. Dimitre Novatchev's FXSL also provides a simple means of retrieving a maximum value. Let's look at developing our own solution using a recursive named template that implements a recursive quick sort algorithm.
The result is: