Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
Van toepassing op:SQL Server
Azure SQL Managed Instance
Voegt nieuwe schemacomponenten toe aan een bestaande XML-schemacollectie.
Transact-SQL syntaxis-conventies
Syntax
ALTER XML SCHEMA COLLECTION [ relational_schema. ]sql_identifier ADD 'Schema Component'
Arguments
relational_schema
Hiermee wordt de naam van het relationele schema geïdentificeerd. Als dit niet is opgegeven, wordt ervan uitgegaan dat het standaard relationele schema wordt gebruikt.
sql_identifier
Is de SQL-identificatie voor de XML-schemacollectie.
'Schemacomponent'
Is de schemacomponent die ingevoegd moet worden.
Remarks
Gebruik de ALTER XML SCHEMA COLLECTION om nieuwe XML-schema's toe te voegen waarvan de namespaces nog niet in de XML-schemacollectie staan, of voeg nieuwe componenten toe aan bestaande namespaces in de collectie.
Het volgende voorbeeld voegt een nieuw <element> toe aan de bestaande naamruimte https://MySchema/test_xml_schema in de collectie MyColl.
-- First create an XML schema collection.
CREATE XML SCHEMA COLLECTION MyColl AS '
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://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="https://MySchema/test_xml_schema">
<element name="anotherElement" type="byte"/>
</schema>';
ALTER XML SCHEMA voegt element <anotherElement> toe aan de eerder gedefinieerde naamruimte https://MySchema/test_xml_schema.
Let op dat als sommige van de componenten die je in de collectie wilt toevoegen referentiecomponenten verwijzen die al in dezelfde collectie zitten, je moet gebruiken <import namespace="referenced_component_namespace" />. Het is echter niet geldig om de huidige schemanaamruimte in <xsd:import>te gebruiken, en daarom worden componenten uit dezelfde doelnaamruimte als de huidige schemanaamruimte automatisch geïmporteerd.
Om verzamelingen te verwijderen, gebruik DROP XML SCHEMA COLLECTION (Transact-SQL).
Als de schemacollectie al een lakse validatie-wildcard of een element van type xs:anyType bevat, zal het toevoegen van een nieuwe globale element-, type- of attribuutverklaring aan de schemacollectie leiden tot een hervalidatie van alle opgeslagen gegevens die door de schemacollectie worden beperkt.
Permissions
Om een XML SCHEMA COLLECTION te wijzigen vereist ALTER-toestemming voor de collectie.
Examples
A. XML-schemaverzameling aanmaken in de database
In het volgende voorbeeld wordt de XML-schemaverzameling ManuInstructionsSchemaCollectiongemaakt. De verzameling heeft slechts één schemanaamruimte.
-- 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="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"
xmlns ="https://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;
U kunt de schemaverzameling ook als volgt toewijzen aan een variabele en de variabele in de CREATE XML SCHEMA COLLECTION instructie als volgt opgeven:
DECLARE @MySchemaCollection nvarchar(max);
SET @MySchemaCollection = N' copy the schema collection here';
CREATE XML SCHEMA COLLECTION AS @MySchemaCollection;
De variabele in het voorbeeld is van nvarchar(max) het type. De variabele kan ook van het xml-datatype zijn, in welk geval deze impliciet wordt omgezet in een string.
Zie Een opgeslagen XML-schemaverzameling weergeven voor meer informatie.
Je kunt schemacollecties opslaan in een xml-kolom . Voer in dit geval de volgende stappen uit om een XML-schemaverzameling te maken:
Haal de schemacollectie uit de kolom op met een SELECT-instructie en wijs deze toe aan een variabele van xml-type , of een varchar-type .
Geef de naam van de variabele op in de CREATE XML SCHEMA COLLECTION instructie.
De slaat CREATE XML SCHEMA COLLECTION alleen de schemacomponenten op die SQL Server begrijpt; alles in het XML-schema wordt niet opgeslagen in de database. Daarom raden we aan om de XML-schemacollectie precies terug te krijgen zoals deze is geleverd, je XML-schema's op te slaan in een databasekolom of een andere map op je computer.
B. Het specificeren van meerdere schemanaamruimtes in een schemacollectie
U kunt meerdere XML-schema's opgeven wanneer u een XML-schemaverzameling maakt. Voorbeeld:
CREATE XML SCHEMA COLLECTION N'
<xsd:schema>....</xsd:schema>
<xsd:schema>...</xsd:schema>';
In het volgende voorbeeld wordt de XML-schemaverzameling ProductDescriptionSchemaCollection gemaakt die twee XML-schemanaamruimten bevat.
CREATE XML SCHEMA COLLECTION ProductDescriptionSchemaCollection AS
'<xsd:schema targetNamespace="https://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="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription"
xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription"
elementFormDefault="qualified"
xmlns:mstns="https://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wm="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain" >
<xs:import
namespace="https://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. Importeren van een schema dat geen doelnaamruimte specificeert
Als een schema dat geen targetNamespace-attribuut bevat wordt geïmporteerd in een collectie, worden de componenten ervan gekoppeld aan de lege doeltekenreeks zoals getoond in het volgende voorbeeld. Let op dat het niet koppelen van één of meer schema's die in de collectie worden geïmporteerd, resulteert in meerdere schemacomponenten (mogelijk niet-gerelateerd) die gekoppeld zijn aan de standaard lege stringnaamruimte.
-- 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='';
Zie ook
CREATE XML SCHEMA COLLECTION (Transact-SQL)
DROP XML SCHEMA COLLECTION (Transact-SQL)
EVENTDATA (Transact-SQL)
Vergelijk getypte XML met niet-getypte XML
Vereisten en beperkingen voor XML-schemaverzamelingen op de server