tags:

views:

856

answers:

3

Hey,

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 a string, but it would be nice to store it as a real GUID. Any ideas how to do it.

regards scope_creep.

A: 

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
I saw the article for that as well.How do you link the SimpleType into your element, attribute xml, say<rule ruleguid="10122-xxx-...."/>
scope_creep
+2  A: 

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>

Cheers! Marc

marc_s
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
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
A: 

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

xs:simpleType name="GUID">

<xs:element name="ruleident">
<xs:complexType>
  <xs:attribute name="ruleGuid" >
    <xs:simpleType>
 <xs:restriction base ="GUID">
        </xs:restriction>
     </xs:simpleType>
  </xs:attribute >
</xs:complexType >

scope_creep