Menu

To store the typed XML data, programmer need to first register the schema associated with the data in the XML schema collection objects in the database. The XML schema collection is an object on the SQL Server that is used to save one or more XML schemas. You can create an XML schema collection object by using the following SQL statement:

CREATE XML SCHEMA COLLECTION <Name> as Expression
Where,

Name specifies an identifier name with which the SQL Server will identify the schema collection.

Expression specifies an XML value that contains one or more XML schema documents

For example, the customer details are associated with the following schema:<?XML version=”1.0” ?><xsd:schema targetNamespace=”http//schemas.adventure-Works.com/Customers” xmlns=”http//shemas.adventure-Works.com/Customers” elementFormDefault=”qualified”attributeFormDefault=” unqualified”xmlns:xsd=”http://www.w3.org/2001/XMLSchema”?><xsd:element name =”Customer” type=”xsd:string” /><xsd:attribute name=”City” type=”xsd:string” /></xsd:schema>
You can use the following statements to register the preceding schema, named as CustomerSchemaCollection, with the database:

You can view information about the registered schemas in a database by querying the sys.XML_schema_collection catalog view, as shown in the following statement:SELECT * FROM sys.XML_schema_collections

After registering the XML schema, you can use the schemas to validate typed XML values while inserting records into the tables. You need to specify this while creating a table that will store the XML data. In the preceding example, if you need to validate the customer details with the CustomerSchemaCollection schema, you need to create the CustDetails table by using the following statement:

CREATE TABLE CustDetails(CustID int,CustDetails XML)

You can insert data into this table by using the following statement:INSERT INTO CustDetails VALUES (2, ‘<?xml version=”1.0”?> <CustomerName=”Abrahim Jones” City=”Selina” />’)

While executing the preceding statement, the SQL Server will validate the values for the CustDetails column against the CustomerSchemaCollection schema.