An XML schema can be defined for an xml variable or an xml column. Then SQL Server will automatically validate the content against this xml schema.

1. Defining a schema

create xml schema collection DocumentXmlSchemaCollection as
N'<?xml version="1.0" encoding="utf-16"?>
<xs:schema targetNamespace="http://www.rms.ro/XmlSchemas/Documents"
    elementFormDefault="qualified"
    xmlns="http://www.rms.ro/XmlSchemas/Documents"
    xmlns:mstns="http://www.rms.ro/XmlSchemas/Documents"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:complexType name="TDocument">
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="1" type="xs:int" name="Id"></xs:element>
      <xs:element minOccurs="1" maxOccurs="1" type="xs:string" name="Name"></xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:element type="TDocument" name="Document"></xs:element>
</xs:schema>
'
Advertisements