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.

VN:F [1.9.22_1171]
Rating: 8.9/10 (7 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
SQLXML : How to Create an XML Schema, 8.9 out of 10 based on 7 ratings  
Be Sociable, Share!
  • Tweet