How can I generate a UUID with pure XSLT? Basically looking for a way to create unique sequences with XSLT. The sequence can be any length.
I'm using XSLT 2.0.
How can I generate a UUID with pure XSLT? Basically looking for a way to create unique sequences with XSLT. The sequence can be any length.
I'm using XSLT 2.0.
You can use xslt snippet for this (source: http://code.google.com/p/public-contracts-ontology/source/browse/transformers/GB-notices/uuid.xslt?r=66e1d39a1c140079a86d219df5b3e031007cc957):
For generating random numbers in XSLT, see Casting the Dice with FXSL: Random Number Generation Functions in XSLT. The only extension function it uses is node-set(), which is no longer necessary in XSLT 2.0.
Also, if the requirement is only that the IDs be unique (not necessarily random), take a look at how to generate unique string. For example if you are generating a UUID for each element of an input XML document, you can use a combination of the URL of the input document, and
<xsl:number>
to generate a unique string for each element.Here's a good example. Basically you set up an extension that points to the java UUID class, and then reference it in the XSL:
Take a look to another question Generate GUID in XSLT.
Probably this article will help you - there defined XSLT functions to generate GUID
If using
.Net
'sXslCompiledTransform
to transform your XSL, you can set theEnableScripts
property totrue
, then use code such as below:NB: I've given this custom functionality the name/prefix
csharp
in the above; but you can call it whatever you like.For more on enabling scripts, see https://stackoverflow.com/a/1873265/361842.
Full XSLT file below to give some additional context:
Since XSLT is a functional language, generating random numbers is not part of the language. That said, there are extension packages (EXSLT) and some processors (Saxon) that support generation of random numbers. If you can't use extensions or Saxon, then I believe you're out of luck.