How to generate GUIDs in XSLT 2.0, using Saxon.NET

I have a project where I need to generate file names that are GUIDs. I’m using Saxon.NET. With the help of Google, I figured out how to do that, but it wasn’t immediately obvious. Hopefully this post will make the solution easier to find for other people trying to solve the same problem.

This is just one example of a .NET function you can access as an extension function in XSLT 2.0, so a more general-purpose treatment of .NET extension functions might be more useful. (I defer to “Writing extension functions for .NET” for that.) But if you’re like me, you spend most of your coding time within the safe, comfy confines of pure XSLT. And you do very little .NET development. So to ensure your continued comfort, here’s how you can generate a GUID in Saxon.NET (relevant parts highlighted):

<xsl:stylesheet version="2.0"

  <xsl:output indent="yes"/>

  <xsl:template match="/">
        <xsl:value-of select="guid:NewGuid()"/>
        <xsl:value-of select="guid:NewGuid()"/>
        <xsl:value-of select="guid:NewGuid()"/>


Here’s an example result from applying this stylesheet (to any input document):


All you do is call the .NET platform’s System.Guid.NewGuid() method as an extension function. The function’s namespace URI identifies for Saxon what assembly and object class you’re interested in (System.Guid in this case).

Thanks to M. David Peterson: I found this usage buried in a code example in his lucidly-titled blog post: if ((OOP + FP + AOP) == XSLT 2.0) then ‘Composable Language’ else ‘Try Again’.


  1. Kumana said,

    December 14, 2009 @ 5:32 pm

    I tried this with Saxon-HE 9.2 and get the following error.

    Cannot find a matching 0-argument function named

    Do you know what might be the issue?


  2. Pete said,

    May 31, 2010 @ 9:54 am

    Saxon version 9.2 no longer seems to support this. I tried the previous version and it worked fine.

RSS feed for comments on this post · TrackBack URI

Leave a Comment