On 11/3/2022 8:00 PM, Graydon Saunders wrote:


You can (I think) test if some attribute value is an RFC 4122 UUID by using a regular expression:

let $regexp as xs:string := '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-5][0-9a-fA-F]{3}-[089abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'

for $uuid in //@GUID
  let $value as xs:string := $uuid/string()
  where not(matches($value,$regexp))
  return $value

Is there a better way to do this?

I wondered whether Java has a class or method that could help and tried

declare namespace uuid = "java:java.util.UUID";

declare function local:isUUID($uuid as xs:string) as xs:boolean {
  try {
    let $uuidJava := uuid:fromString($uuid)
    return true()
  } catch * {
    false()
  }
};

("123e4567-e89b-12d3-a456-9AC7CBDCEE52",
"123e4567-h89b-12d3-a456-9AC7CBDCEE52") ! local:isUUID(.)

Note I haven't checked whether Java implements the RFC you mention.