sql XML Schema Collection,SQL Server中的Type vs Untyped XML
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql XML Schema Collection,SQL Server中的Type vs Untyped XML相关的知识,希望对你有一定的参考价值。
/*
In SQL Server, XML schemas are collected into an XML Schema collection. These are the schemas that you want to apply to XML data stored in an XML data type (a column, a variable, etc.).
The XML schema collection:
- stores the imported XML schemas
- validates XML data type instances
- types the XML data as it is stored in the database (enforces the types of data that need to be present in the XML)
***Untyped XML (unvalidated) vs. Typed XML (validated) in SQL Server
Use the untyped XML data type when:
- you do not have a schema for your XML data
- you have schemas, but don't want the server to validate data (there is significant overhead impact on a a server performing validation)
Use the typed XML data type when:
- you have schemas and want the server to validate the XML data
- you want to take advantage of storage and query optimizations based on type information
- you want to take advantage of type infomraiton during compliation of your queries (SQL Server can be more efficient based on the type information it has, can be more efficient in how XML data is stored)
*/
-- example of creating an XML Schema Collection named AWItems2
CREATE XML SCHEMA COLLECTION AWItems2
AS
N'<xs:schema targetNamespace="http://aw/orderschema"
xmlns="http://aw/orderschema"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:complexType name="person">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:positiveInteger" use="required"/>
</xs:complexType>
<xs:complexType name="itemlist">
<xs:sequence>
<xs:element name="item" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:positiveInteger"/>
<xs:attribute name="Quantity" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>';
GO
-- how to view details of the schema collection created, including a table that shows the elements and attributes created and information about them.
SELECT cp.*
FROM sys.xml_schema_componenets cp
JOIN sys.xml_chema_collections c
ON cp.xml_collection_id = c.xml_collection_id
WHERE c.name = 'AWItems';
-- how to create an XML column that is to be associated with the AWItems schema collection created above. This means any XML inserted into this XML column will be validated against the schema collection. If you try to insert XML data that is unqualified (note associated with the namespace of the schema) or invalid you'll get an XML Validation error and it won't let you insert it.
CREATE TABLE dbo.SalesOrder (
OrderID INT IDENTITY PRIMARY KEY CLUSTERED,
OrderDate date DEFAULT GETDATE(),
-- create XML column which is to be associated with the AWItems schema collection
OrderItems XML (AWItems));
-- how to create an XML column that is to be associated with the AWItems schema created above, but also make it so the column will not accept fragments (it'll only accept well-formed XML documents
CREATE TABLE dbo.SalesOrder (
OrderID INT IDENTITY PRIMARY KEY CLUSTERED,
OrderDate date DEFAULT GETDATE(),
-- create XML column which is to be associated with the AWItems schema collection - DOCUMENT tells it to only accept well-formed XML documents that have a root element (ie no fragments allowed)
OrderItems XML (DOCUMENT AWItems));
以上是关于sql XML Schema Collection,SQL Server中的Type vs Untyped XML的主要内容,如果未能解决你的问题,请参考以下文章