debugsqlMinor
How to resolve this 'XML Validation: Declaration not found for element' error?
Viewed 0 times
thiserrordeclarationvalidationxmlfoundresolveforelementhow
Problem
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 -
I did this similar to what is mentioned here.
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?
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 ('
1
Chai
1
1
10 boxes x 20 bags\
18.0000
39
0
10
0
');
-- Create XML Schema
CREATE XML SCHEMA COLLECTION XS_ProductDoc
AS '
';
-- 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?
Solution
An XML Schema Collection for that particular piece of XML would look more like this:
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.
CREATE XML SCHEMA COLLECTION XS_ProductDoc
AS
'
'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.
Code Snippets
CREATE XML SCHEMA COLLECTION XS_ProductDoc
AS
'<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Product">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="ProductID" type="xs:unsignedByte" />
<xs:element name="ProductName" type="xs:string" />
<xs:element name="SupplierID" type="xs:unsignedByte" />
<xs:element name="CategoryID" type="xs:unsignedByte" />
<xs:element name="QuantityPerUnit" type="xs:string" />
<xs:element name="UnitPrice" type="xs:decimal" />
<xs:element name="UnitsInStock" type="xs:unsignedByte" />
<xs:element name="UnitsOnOrder" type="xs:unsignedByte" />
<xs:element name="ReorderLevel" type="xs:unsignedByte" />
<xs:element name="Discontinued" type="xs:unsignedByte" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>'Context
StackExchange Database Administrators Q#133815, answer score: 6
Revisions (0)
No revisions yet.