您的位置:首页 > 数据库

sql server 2005 T-SQL ALTER XML SCHEMA COLLECTION (Transact-SQL)

2007-12-21 10:09 831 查看
向现有 XML 架构集合中添加新架构组件。


Transact-SQL 语法约定


语法

ALTER XML SCHEMA COLLECTION [ relational_schema. ]sql_identifier ADD 'Schema Component'



参数

relational_schema
标识关系架构的名称。如果未指定,则假定为默认的关系架构。

sql_identifier
是 XML 架构集合的 SQL 标识符。

'Schema Component'
要插入的架构组件。


备注

使用 ALTER XML SCHEMA COLLECTION 添加其命名空间尚不在 XML 架构集合中的新 XML 架构,或向已存在于该集合的命名空间中添加新组件。

以下示例将新的 <element> 添加到集合 MyColl 的现有命名空间 http://MySchema/test_xml_schema 中。



复制代码


-- First create an XML schema collection.
CREATE XML SCHEMA COLLECTION MyColl AS '
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://MySchema/test_xml_schema">
<element name="root" type="string"/>
</schema>'
-- Modify the collection.
ALTER XML SCHEMA COLLECTION MyColl ADD '
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://MySchema/test_xml_schema">
<element name="anotherElement" type="byte"/>
</schema>'


ALTER XML SCHEMA 将元素 <anotherElement> 添加到以前定义的命名空间 http://MySchema/test_xml_schema 中。

注意,如果要在集合中添加的某些组件引用同一集合中已经存在的组件,则必须使用 <import namespace="referenced_component_namespace" />。但是,在 <xsd:import> 中使用当前架构命名空间是无效的,因此将自动导入与当前架构命名空间相同的目标命名空间中的组件。

若要删除集合,请使用 DROP XML SCHEMA COLLECTION (Transact-SQL)


权限

更改 XML SCHEMA COLLECTION 需要对集合具有 ALTER 权限。

有关详细信息,请参阅XML 架构集合的权限


示例

A. 在数据库中创建 XML 架构集合

下面的示例将创建 XML 架构集合 ManuInstructionsSchemaCollection。该集合只有一个架构命名空间。



复制代码


-- Create a sample database in which to load the XML schema collection.
CREATE DATABASE SampleDB
GO
USE SampleDB
GO
CREATE XML SCHEMA COLLECTION ManuInstructionsSchemaCollection AS
N'<?xml version="1.0" encoding="UTF-16"?>
<xsd:schema targetNamespace="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"
xmlns          ="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

<xsd:complexType name="StepType" mixed="true" >
<xsd:choice  minOccurs="0" maxOccurs="unbounded" >
<xsd:element name="tool" type="xsd:string" />
<xsd:element name="material" type="xsd:string" />
<xsd:element name="blueprint" type="xsd:string" />
<xsd:element name="specs" type="xsd:string" />
<xsd:element name="diag" type="xsd:string" />
</xsd:choice>
</xsd:complexType>

<xsd:element  name="root">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="Location" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="step" type="StepType" minOccurs="1" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="LocationID" type="xsd:integer" use="required"/>
<xsd:attribute name="SetupHours" type="xsd:decimal" use="optional"/>
<xsd:attribute name="MachineHours" type="xsd:decimal" use="optional"/>
<xsd:attribute name="LaborHours" type="xsd:decimal" use="optional"/>
<xsd:attribute name="LotSize" type="xsd:decimal" use="optional"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>' ;
GO
-- Verify - list of collections in the database.
SELECT *
FROM sys.xml_schema_collections
-- Verify - list of namespaces in the database.
SELECT name
FROM sys.xml_schema_namespaces

-- Use it. Create a typed xml variable. Note the collection name
-- that is specified.
DECLARE @x xml (ManuInstructionsSchemaCollection)
GO
--Or create a typed xml column.
CREATE TABLE T (
i int primary key,
x xml (ManuInstructionsSchemaCollection))
GO
-- Clean up.
DROP TABLE T
GO
DROP XML SCHEMA COLLECTION ManuInstructionsSchemaCollection
Go
USE master
GO
DROP DATABASE SampleDB


另外,您还可以将架构集合分配给一个变量,并按如下方式在 CREATE XML SCHEMA COLLECTION 语句中指定该变量:



复制代码


DECLARE @MySchemaCollection nvarchar(max)
Set @MySchemaCollection  = N' copy the schema collection here'
CREATE XML SCHEMA COLLECTION AS @MySchemaCollection


示例中的变量为 nvarchar(max) 类型。该变量也可以为 xml 数据类型,在这种情况下,它将隐式转换为字符串。

有关详细信息,请参阅查看存储 XML 架构集合

可在 xml 类型列中存储架构集合。在这种情况下,若要创建 XML 架构集合,请执行以下步骤:

使用 SELECT 语句从列中检索该架构集合,然后将它分配给一个类型为 xmlvarchar 的变量。

在 CREATE XML SCHEMA COLLECTION 语句中指定变量名称。

CREATE XML SCHEMA COLLECTION 只存储 SQL Server 熟悉的架构组件;XML 架构中的所有内容均不存储在数据库中。因此,如果您希望 XML 架构集合保持提供它时的原样,建议您在数据库列或计算机上的其他文件夹中保存您的 XML 架构。

B. 在架构集合中指定多个架构命名空间

在创建 XML 架构集合时,可以指定多个 XML 架构。例如:



复制代码


CREATE XML SCHEMA COLLECTION N'
<xsd:schema>....</xsd:schema>
<xsd:schema>...</xsd:schema>'


下面的示例将创建包含两个 XML 架构命名空间的 XML 架构集合 ProductDescriptionSchemaCollection。



复制代码


CREATE XML SCHEMA COLLECTION ProductDescriptionSchemaCollection AS
'<xsd:schema targetNamespace="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain"
xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain"
elementFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<xsd:element name="Warranty"  >
<xsd:complexType>
<xsd:sequence>
<xsd:element name="WarrantyPeriod" type="xsd:string"  />
<xsd:element name="Description" type="xsd:string"  />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<xs:schema targetNamespace="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription"
xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription"
elementFormDefault="qualified"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wm="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain" >
<xs:import
namespace="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain" />
<xs:element name="ProductDescription" type="ProductDescription" />
<xs:complexType name="ProductDescription">
<xs:sequence>
<xs:element name="Summary" type="Summary" minOccurs="0" />
</xs:sequence>
<xs:attribute name="ProductModelID" type="xs:string" />
<xs:attribute name="ProductModelName" type="xs:string" />
</xs:complexType>
<xs:complexType name="Summary" mixed="true" >
<xs:sequence>
<xs:any processContents="skip" namespace="http://www.w3.org/1999/xhtml" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>'
;
GO
-- Clean up
DROP XML SCHEMA COLLECTION ProductDescriptionSchemaCollection
GO


C. 导入未指定目标命名空间的架构

如果向集合中导入未包含 targetNamespace 属性的架构,该架构的组件将与空字符串目标命名空间相关联,如下面的示例所示。注意,如果在集合中导入的一个或多个架构之间没有任何关联,将导致多个架构组件(可能不相关)都与默认的空字符串命名空间关联。



复制代码


-- Create a collection that contains a schema with no target namespace.
CREATE XML SCHEMA COLLECTION MySampleCollection AS '
<schema xmlns="http://www.w3.org/2001/XMLSchema"  xmlns:ns="http://ns">
<element name="e" type="dateTime"/>
</schema>'
GO
-- query will return the names of all the collections that
--contain a schema with no target namespace
SELECT sys.xml_schema_collections.name
FROM   sys.xml_schema_collections
JOIN   sys.xml_schema_namespaces
ON     sys.xml_schema_collections.xml_collection_id =
sys.xml_schema_namespaces.xml_collection_id
WHERE  sys.xml_schema_namespaces.name=''
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: