SQLXML : How to Create an XML Schema

The following is an example of how to create an XML Schema in SQL Server.

   1: -- DROP IF EXISTS
   2: IF    EXISTS (SELECT 1 
   3:               FROM  sys.xml_schema_collections 
   4:               WHERE name='SampleSchema')
   5:    DROP XML SCHEMA COLLECTION SampleSchema
   6:  
   7: CREATE XML SCHEMA COLLECTION SampleSchema AS 
   8: '<?xml version="1.0" encoding="utf-8"?>
   9: <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  10:   <xsd:element name="Sample">
  11:     <xsd:complexType>
  12:       <xsd:attribute name="SampleID" type="xsd:integer" />
  13:       <xsd:attribute name="Name" type="xsd:string" />
  14:       <xsd:attribute name="Description" type="xsd:string" />
  15:     </xsd:complexType>
  16:   </xsd:element>
  17: </xsd:schema>'

 

To check your XML Schema:

Method 1: Go to your database > Programmability > Types > XML Schema Collections

SQLXML - XML Schema Collection

Method 2: You can use the xml_schema_namespace function to query the schema from within SSMS

   1:  
   2: SELECT
   3:    xml_schema_namespace(N'dbo',N'SampleSchema') 

 

Later on I will post additional sample schemas which use different SQL XML data types.

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s