How to resolve this 'XML Validation: Declaration not found for element' error? I'm new to SQL Server and I have this task to complete (for learning purposes), yet I'm unable to figure out this error due to inexperience with XML Schemas (I think)

Here's what I'm trying to do -


Create a table with 2 columns - ID, XML info for some product (non-typed).
Add a row entry, that is, XML data for a product.
Create an XML Schema for the XML-product-data.
Bind this schema to the one (column) in the table.


I did this similar to what is mentioned here.

-- Create table ProductDocs
CREATE TABLE ProductDocs
(
    ID INT IDENTITY PRIMARY KEY,
    ProductDoc XML NOT NULL
);

-- Insert data into ProductDocs
INSERT INTO ProductDocs
(ProductDoc)
VALUES ('&lt;Product&gt;
            &lt;ProductID&gt;1&lt;/ProductID&gt;
            &lt;ProductName&gt;Chai&lt;/ProductName&gt;
            &lt;SupplierID&gt;1&lt;/SupplierID&gt;
            &lt;CategoryID&gt;1&lt;/CategoryID&gt;
            &lt;QuantityPerUnit&gt;10 boxes x 20 bags&lt;/QuantityPerUnit&gt;\
            &lt;UnitPrice&gt;18.0000&lt;/UnitPrice&gt;
            &lt;UnitsInStock&gt;39&lt;/UnitsInStock&gt;
            &lt;UnitsOnOrder&gt;0&lt;/UnitsOnOrder&gt;
            &lt;ReorderLevel&gt;10&lt;/ReorderLevel&gt;
            &lt;Discontinued&gt;0&lt;/Discontinued&gt;
        &lt;/Product&gt;');


-- Create XML Schema
CREATE XML SCHEMA COLLECTION XS_ProductDoc 
AS '&lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               targetNamespace="http://www.microsoft.com/schemas/adventure-works/products"
               xmlns:prod="http://www.microsoft.com/schemas/adventure-works/products"&gt;

        &lt;xs:element name="Product"&gt;
            &lt;xs:complexType&gt;
                &lt;xs:sequence&gt;
                    &lt;xs:element ref="prod:ProductID" /&gt;
                    &lt;xs:element ref="prod:ProductName" /&gt;
                    &lt;xs:element ref="prod:SupplierID" /&gt;
                    &lt;xs:element ref="prod:CategoryID" /&gt;
                    &lt;xs:element ref="prod:QuantityPerUnit" /&gt;
                    &lt;xs:element ref="prod:UnitPrice" /&gt;
                    &lt;xs:element ref="prod:UnitsInStock" /&gt;
                    &lt;xs:element ref="prod:UnitsOnOrder" /&gt;
                    &lt;xs:element ref="prod:ReorderLevel" /&gt;
                    &lt;xs:element ref="prod:Discontinued" /&gt; 
                &lt;/xs:sequence&gt;
            &lt;/xs:complexType&gt;
        &lt;/xs:element&gt;

        &lt;xs:element name="ProductID" type="xs:integer" /&gt;
        &lt;xs:element name="ProductName" type="xs:string" /&gt;
        &lt;xs:element name="SupplierID" type="xs:integer" /&gt;
        &lt;xs:element name="CategoryID" type="xs:integer" /&gt;
        &lt;xs:element name="QuantityPerUnit" type="xs:string" /&gt;
        &lt;xs:element name="UnitPrice" type="xs:double" /&gt;
        &lt;xs:element name="UnitsInStock" type="xs:integer" /&gt;
        &lt;xs:element name="UnitsOnOrder" type="xs:integer" /&gt;
        &lt;xs:element name="ReorderLevel" type="xs:integer" /&gt;
        &lt;xs:element name="Discontinued" type="xs:boolean" /&gt;

    &lt;/xs:schema&gt;';

-- Bind the schema
ALTER TABLE ProductDocs
ALTER COLUMN ProductDoc xml (XS_ProductDoc);


But I'm getting this error -


  XML Validation: Declaration not found for element 'Product'. Location:
  /*:Product[1] The statement has been terminated.


Why is this error occurring?
And, how can I resolve it?
An XML Schema Collection for that particular piece of XML would look more like this:

CREATE XML SCHEMA COLLECTION XS_ProductDoc 
AS
'&lt;xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;xs:element name="Product"&gt;
        &lt;xs:complexType mixed="true"&gt;
            &lt;xs:sequence&gt;
                &lt;xs:element name="ProductID" type="xs:unsignedByte" /&gt;
                &lt;xs:element name="ProductName" type="xs:string" /&gt;
                &lt;xs:element name="SupplierID" type="xs:unsignedByte" /&gt;
                &lt;xs:element name="CategoryID" type="xs:unsignedByte" /&gt;
                &lt;xs:element name="QuantityPerUnit" type="xs:string" /&gt;
                &lt;xs:element name="UnitPrice" type="xs:decimal" /&gt;
                &lt;xs:element name="UnitsInStock" type="xs:unsignedByte" /&gt;
                &lt;xs:element name="UnitsOnOrder" type="xs:unsignedByte" /&gt;
                &lt;xs:element name="ReorderLevel" type="xs:unsignedByte" /&gt;
                &lt;xs:element name="Discontinued" type="xs:unsignedByte" /&gt;
            &lt;/xs:sequence&gt;
        &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
&lt;/xs:schema&gt;'


The reason your code does not work is because your XML does not contain any namespaces, whereas the original AdventureWorks XML does.  Learn more about XML Namespaces here.