Thanks, Tim. I had a side-bet on whether or not anyone would spot that, and I'm glad you did.

Here's how I'd fix, just for comparison:

  string-join(
    for $i in 1 to 2
    let $v := xdmp:integer-to-hex(xdmp:random())
    let $len := string-length($v)
    return
      if ($len eq 32) then $v
      else concat(string-pad('0', 16 - $len), $v)
    , ''
  )

-- Mike

Tim Finney wrote:
Michael's solution won't work if xdmp:random() happens to return two
small numbers. For example, the following produces nothing:

let $x := concat(
  xdmp:integer-to-hex(random(100)),
  xdmp:integer-to-hex(random(100))
)
return substring($x, 21, 12)

It can be fixed by testing for a minimum string length for $x then
calling the function again if the test fails:

return
  if (string-length($x < 32))
  then generate-uuid-v4()
  else string-join((substring($x, 1, 8),...), "-")



Otherwise, you could try something like this


(: Functions :)

define function random-hex(
  $length as xs:integer
) as xs:string {
  string-join(
    for $n in 1 to $length
    return xdmp:integer-to-hex(xdmp:random(15)),
    ""
  )
}

define function generate-uuid-v4() as xs:string {
  string-join(
    (
      random-hex(8),
      random-hex(4),
      random-hex(4),
      random-hex(4),
      random-hex(12)
    ),
    "-"
  )
}

(: Query :)

generate-uuid-v4()



Best

Tim Finney
UVa Press

On Fri, 2007-08-10 at 16:01 -0700, Michael Blakeley wrote:
I happen to have one in front of me right now. Note that there are at least five flavors of UUID - I chose to implement type-4.

(: this is a v4 UUID :)
define function generate-uuid-v4()
  as xs:string
{
   let $x := concat(
     xdmp:integer-to-hex(xdmp:random()),
     xdmp:integer-to-hex(xdmp:random())
   )
   return string-join((
     substring($x, 1, 8), substring($x, 9, 4),
     substring($x, 13, 4), substring($x, 17, 4), substring($x, 21, 14)
     ), '-'
   )
}

Ref: http://en.wikipedia.org/wiki/UUID

-- Mike

Gary Vidal wrote:
I was wondering if anybody has implemented a uuid (universally unique
identifier) function in Xquery.  I would like to use this to generate
id's or a comparable id generation scheme

Regards,

Gary Vidal

Sr. .Net Developer

Tel: 212-592-4946

[EMAIL PROTECTED]




------------------------------------------------------------------------

_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

Reply via email to