16

I'm trying to create an XML schema, which enables an attribute value to be stored as an GUID in native format. I could set it up as a string, but it would be nice to store it as a real GUID.

Any ideas how to do it?

informatik01
  • 16,038
  • 10
  • 74
  • 104
scope_creep
  • 4,213
  • 11
  • 35
  • 64

3 Answers3

17

You can define your own custom simple type "GUID" by restricting a string using a regular expression like this:

<xs:simpleType name="GUID">
  <xs:restriction base="xs:string">
    <xs:pattern value="([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})|(\{[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\})"/>
  </xs:restriction>
</xs:simpleType>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Well i've tried that but it keeps coming back with a 'The Pattern constraint failed.' in file: error message when it encounters it. – scope_creep Jun 21 '09 at 21:16
  • 1
    Odd... we have this in production and everything works just fine.... when and how are you using it, and where does the error occur? – marc_s Jun 22 '09 at 04:59
5

XML basically contains only strings, although XSD also defines certain other primitive types. GUID, however, is not among them.

You can define your own schema for a GUID type. Lots of people have done this. Here's how the Microsoft OneNote team did it: http://msdn.microsoft.com/en-us/library/aa203890(office.11).aspx.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
4

I've sussed it out. Sometimes it helps to read the docs. This is how it will work.

    <xs:simpleType name="GUID">
      <xs:restriction base="xs:string">
        <xs:pattern value="([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})|(\{[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\})"/>
      </xs:restriction>
    </xs:simpleType>


    <xs:element name="ruleident">
      <xs:complexType>
        <xs:attribute name="ruleGuid" >
          <xs:simpleType>
            <xs:restriction base ="GUID"/>
           </xs:simpleType>
        </xs:attribute >
      </xs:complexType >
    </xs:element>
Digital Alpha
  • 226
  • 1
  • 12
scope_creep
  • 4,213
  • 11
  • 35
  • 64