Pal*_*shV 5 xml sql-server sql-server-2012 xsd
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.
-- Create table ProductDocs
CREATE TABLE ProductDocs
(
ID INT IDENTITY PRIMARY KEY,
ProductDoc XML NOT NULL
);
-- Insert data into ProductDocs
INSERT INTO ProductDocs
(ProductDoc)
VALUES ('<Product>
<ProductID>1</ProductID>
<ProductName>Chai</ProductName>
<SupplierID>1</SupplierID>
<CategoryID>1</CategoryID>
<QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>\
<UnitPrice>18.0000</UnitPrice>
<UnitsInStock>39</UnitsInStock>
<UnitsOnOrder>0</UnitsOnOrder>
<ReorderLevel>10</ReorderLevel>
<Discontinued>0</Discontinued>
</Product>');
-- Create XML Schema
CREATE XML SCHEMA COLLECTION XS_ProductDoc
AS '<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">
<xs:element name="Product">
<xs:complexType>
<xs:sequence>
<xs:element ref="prod:ProductID" />
<xs:element ref="prod:ProductName" />
<xs:element ref="prod:SupplierID" />
<xs:element ref="prod:CategoryID" />
<xs:element ref="prod:QuantityPerUnit" />
<xs:element ref="prod:UnitPrice" />
<xs:element ref="prod:UnitsInStock" />
<xs:element ref="prod:UnitsOnOrder" />
<xs:element ref="prod:ReorderLevel" />
<xs:element ref="prod:Discontinued" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ProductID" type="xs:integer" />
<xs:element name="ProductName" type="xs:string" />
<xs:element name="SupplierID" type="xs:integer" />
<xs:element name="CategoryID" type="xs:integer" />
<xs:element name="QuantityPerUnit" type="xs:string" />
<xs:element name="UnitPrice" type="xs:double" />
<xs:element name="UnitsInStock" type="xs:integer" />
<xs:element name="UnitsOnOrder" type="xs:integer" />
<xs:element name="ReorderLevel" type="xs:integer" />
<xs:element name="Discontinued" type="xs:boolean" />
</xs:schema>';
-- Bind the schema
ALTER TABLE ProductDocs
ALTER COLUMN ProductDoc xml (XS_ProductDoc);
Run Code Online (Sandbox Code Playgroud)
But I'm getting this error -
XML 验证:未找到元素“产品”的声明。位置:/*:Product[1] 语句已终止。
为什么会发生此错误?而且,我该如何解决?
该特定 XML 片段的 XML 模式集合看起来更像这样:
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>'
Run Code Online (Sandbox Code Playgroud)
您的代码不起作用的原因是您的 XML 不包含任何命名空间,而原始 AdventureWorks XML 包含。在此处了解有关 XML 命名空间的更多信息。
归档时间: |
|
查看次数: |
3200 次 |
最近记录: |