Creating XML Schema Collection

CREATE XML SCHEMA COLLECTION [MyInstructionsSchemaCollection] AS
N'<?xml version="1.0" encoding="UTF-16"?>
<xsd:schema 
	targetNamespace="<http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions>"
	xmlns="<http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions>"
	elementFormDefault="qualified"
	attributeFormDefault="unqualified"
	xmlns:xsd="<http://www.w3.org/2001/XMLSchema>" >
 
	<xsd:complexType name="StepType" mixed="true">
       <xsd:choice minOccurs="0" maxOccurs="unbounded" >
           <xsd:element name="tool" type="xsd:string" />
           <xsd:element name="material" type="xsd:string" />
           <xsd:element name="blueprint" type="xsd:string" />
           <xsd:element name="specs" type="xsd:string" />
           <xsd:element name="diag" type="xsd:string" />
       </xsd:choice>
	</xsd:complexType>
 
	<xsd:element name="root">
       <xsd:complexType mixed="true">
           <xsd:sequence>
               <xsd:element name="Location" minOccurs="1" maxOccurs="unbounded">
                   <xsd:complexType mixed="true">
                       <xsd:sequence>
                           <xsd:element name="step" type="StepType" minOccurs="1" maxOccurs="unbounded" />
                       </xsd:sequence>
                       <xsd:attribute name="LocationID" type="xsd:integer" use="required"/>
                       <xsd:attribute name="SetupHours" type="xsd:decimal" use="optional"/>
                       <xsd:attribute name="MachineHours" type="xsd:decimal" use="optional"/>
                       <xsd:attribute name="LaborHours" type="xsd:decimal" use="optional"/>
                       <xsd:attribute name="LotSize" type="xsd:decimal" use="optional"/>
                   </xsd:complexType>
               </xsd:element>
           </xsd:sequence>
       </xsd:complexType>
	</xsd:element>
</xsd:schema>';
GO

-- Verify - list of collections in the database.
SELECT * FROM sys.xml_schema_collections;

-- Verify - list of namespaces in the database.
SELECT * FROM sys.xml_schema_namespaces;

-- Use it. Create a typed XML variable. Note collection name specified.
DECLARE @MyXml XML ([MyInstructionsSchemaCollection]);
GO

-- Or create a typed XML column.
/*
CREATE TABLE [MyTable]
(
	[Id] INT PRIMARY KEY,
	[Data] XML (MyInstructionsSchemaCollection)
);
*/

-- Clean up
DROP XML SCHEMA COLLECTION [MyInstructionsSchemaCollection];

XML Data Type

Limitations of the XML Data Type

Select Data as XML

SELECT CONVERT(XML, N'<root> <child/> </root>', 1)
DECLARE @XmlDoc XML = 
(
	SELECT [ProductModelID], [Name]
	FROM [Production].[ProductModel]
	FOR XML AUTO
)