首先,我对这些东西完全陌生,所以请不要强硬。 我正在创建一个 XML 架构:
USE SampleDatabase;
GO
CREATE XML SCHEMA COLLECTION ManuInstructionsSchemaCollection 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:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ProductID" type="xs:integer" />
</xs:schema>
';
GO
将 xml 架构绑定(bind)到表:
CREATE TABLE ProductDocs
(
ID INT IDENTITY PRIMARY KEY,
ProductDoc XML(ManuInstructionsSchemaCollection)
);
将 Xml 数据插入表中:
INSERT INTO ProductDocs
VALUES(
N'<Product>
<ProductID>1</ProductID>
</Product>');
但是,然后它给出了错误:
XML Validation: Declaration not found for element 'Product'. Location: /*:Product[1]
我是否正确绑定(bind)架构?我已经搜索了很多,但我无法弄清楚这些事情。如有任何帮助,我们将不胜感激。
最佳答案
尝试像这样定义您的架构。
CREATE XML SCHEMA COLLECTION ManuInstructionsSchemaCollection AS
'<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="Product">
<xs:complexType>
<xs:sequence>
<xs:element ref="ProductID" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ProductID" type="xs:integer" />
</xs:schema>
';
关于sql-server - sql服务器: Bind Xml schema to a column of a Table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24993524/