Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a GUID simpleType into an XML schema?

Tags:

types

guid

xml

xsd

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?

like image 803
scope_creep Avatar asked Jun 21 '09 19:06

scope_creep


People also ask

What is XML GUID?

A GUID is a 128bit identifier and normally fully random. Depending on the language you are using there are several functions to generate a GUID.

How do you specify the schema of XML file?

The line above: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" tells the XML parser that this document should be validated against a schema. The line: xsi:noNamespaceSchemaLocation="shiporder. xsd" specifies WHERE the schema resides (here it is in the same folder as "shiporder. xml").

How do I change the XML Schema?

To edit a fileSelect the Use XML editor to view and edit the underlying XML Schema file link on the Start View. The XML editor appears with the new file open. Copy the XML Schema sample code from Purchase order schema and paste it to replace the code that was added to the new XSD file by default.

How do I reference XML in XSD?

Reference the XSD schema in the XML document using XML schema instance attributes such as either xsi:schemaLocation or xsi:noNamespaceSchemaLocation. Add the XSD schema file to a schema cache and then connect that cache to the DOM document or SAX reader, prior to loading or parsing the XML document.


2 Answers

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>
like image 130
marc_s Avatar answered Sep 22 '22 03:09

marc_s


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.

like image 41
Mark Seemann Avatar answered Sep 24 '22 03:09

Mark Seemann