MarkLogic 10 and Data Hub 5.0

Latest MarkLogic releases provide a smarter, simpler, and more secure way to integrate data.

Read Blog →


Stay On Top Of Everything MarkLogic

Be the first to know! News, product information, and events delivered straight to your inbox.

Sign Me Up →

Knowledgebase: MarkLogic Server
Generating unique IDs (GUIDs)
28 July 2015 01:30 PM


This article will show you a way to create a GUID using the XQuery language.

What are GUIDs?

A GUID (Globally Unique IDentifier) is expressed as a string and is comprised of groups of hexadecimal characters, each of which are separated into five groups by hyphens.

The format can be represented as:


Where X denotes a hexidecimal value.

Generating a GUID

Version 7 and above

In version 7 and higher, the function sem:uuid-string is available to help in creating a UUID/GUID.

Version 6
xquery version "1.0-ml";

declare private function local:random-hex($seq as xs:integer*) as xs:string+ {
  for $i in $seq return 
    fn:string-join(for $n in 1 to $i
      return xdmp:integer-to-hex(xdmp:random(15)), "")

declare function local:guid() as xs:string {
Example use:
for $i in 1 to 250
Best Practice Tip

If you need to create thousands of GUIDs as part of your content processing architecture, be careful to create a separate URI to represent each unique identifier. When managing all GUIDs within a single URI, you may end up creating a bottleneck with multiple processes waiting on a write lock on this single URI. This is especially true if your working database enables the 'maintain last modified' or 'maintain directory last modified' options. Be sure to scan the server ErrorLog.txt file for any XDMP-DEADLOCK messages, which are indicative of a processing bottleneck.

(4 vote(s))
Not helpful

Comments (0)