기본 제공 XML 스키마 컬렉션 참조(sys)

적용 대상:SQL ServerAzure SQL DatabaseAzure SQL Managed Instance

만드는 모든 데이터베이스에는 sys 관계형 스키마에 미리 정의된 sys XML 스키마 컬렉션이 있습니다. 이러한 미리 정의된 스키마를 예약하며 사용자가 만든 다른 XML 스키마 컬렉션에서 액세스할 수 있습니다. 이러한 미리 정의된 스키마에 사용되는 접두사는 XQuery에서 의미가 있습니다. xml예약된 접두사입니다.

xml = http://www.w3.org/XML/1998/namespace
xs = http://www.w3.org/2001/XMLSchema
xsi = http://www.w3.org/2001/XMLSchema-instance
fn = http://www.w3.org/2004/07/xpath-functions
sqltypes = https://schemas.microsoft.com/sqlserver/2004/sqltypes
xdt = http://www.w3.org/2004/07/xpath-datatypes
(no prefix) = urn:schemas-microsoft-com:xml-sql
(no prefix) = https://schemas.microsoft.com/sqlserver/2004/SOAP

sqltypes 네임스페이스에사용자가 만든 XML 스키마 컬렉션에서 참조할 수 있는 구성 요소가 포함되어 있습니다. Microsoft 웹 사이트 에서 sqltypes스키마를 다운로드할 수 있습니다. 기본 제공 구성 요소에는 다음이 포함됩니다.

  • XSD 형식

  • XML 특성 lang, basespace

  • sqltypes 네임스페이 스의 구성 요소

다음 쿼리는 사용자가 만든 XML 스키마 컬렉션에서 참조할 수 있는 기본 제공 구성 요소를 반환합니다.

SELECT C.name, N.name, C.symbol_space_desc from sys.xml_schema_components C join sys.xml_schema_namespaces N
on ((C.xml_namespace_id = N.xml_namespace_id) AND (C.xml_collection_id = N.xml_collection_id))
join sys.xml_schema_collections SC
on SC.xml_collection_id = C.xml_collection_id
where ((C.xml_collection_id = 1) AND (C.name is not null) AND (C.scoping_xml_component_id is null)
AND (SC.schema_id = 4));
GO

다음 예제에서는 사용자 스키마에서 이러한 구성 요소를 참조하는 방법을 보여 줍니다. CREATE XML SCHEMA COLLECTIONvarchar 네임스페이스에 정의된 sqltypes 유형을 참조하는 XML 스키마 컬렉션을 만듭니다. 이 예제에서는 네임스페이스에 xml 정의된 특성도 참조 lang 합니다.

CREATE XML SCHEMA COLLECTION SC AS '
<schema
   xmlns="http://www.w3.org/2001/XMLSchema"
   targetNamespace="myNS"
   xmlns:ns="myNS"
   xmlns:s="https://schemas.microsoft.com/sqlserver/2004/sqltypes" >
   <import namespace="http://www.w3.org/XML/1998/namespace"/>
   <import namespace="https://schemas.microsoft.com/sqlserver/2004/sqltypes"/>
   <element name="root">
      <complexType>
          <sequence>
             <element name="a" type="string"/>
             <element name="b" type="string"/>
             <!-- varchar type is defined in the sys.sys collection and
                  can be referenced in any user-defined schema -->
             <element name="c" type="s:varchar"/>
          </sequence>
          <attribute name="att" type="int"/>
          <!-- xml:lang attribute is defined in the sys.sys collection
               and can be referenced in any user-defined schema -->
          <attribute ref="xml:lang"/>
      </complexType>
    </element>
</schema>';
GO
-- Cleanup
DROP xml schema collection SC;
GO

다음 사항에 유의해야 합니다.

  • 사용자 정의 XML 스키마 컬렉션에서는 이러한 네임스페이스를 사용하여 XML 스키마를 수정할 수 없습니다. 예를 들어 다음 XML 스키마 컬렉션은 보호된 네임스페이스에 구성 요소를 추가하므로 sqltypes 실패합니다.

    CREATE XML SCHEMA COLLECTION SC AS '
    <schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace
        ="https://schemas.microsoft.com/sqlserver/2004/sqltypes" >
          <element name="root" type="string"/>
    </schema>';
    GO
    
  • XML 스키마 컬렉션을 사용하여 sys 열, 변수 또는 매개 변수를 입력 xml 할 수 없습니다. 예를 들어 다음 코드는 오류를 반환합니다.

    DECLARE @x xml (sys.sys);
    
  • 이러한 기본 제공 스키마의 serialization은 지원되지 않습니다. 예를 들어 다음 코드는 오류를 반환합니다.

    SELECT XML_SCHEMA_NAMESPACE(N'sys', N'sys')l
    GO
    

다음 코드는 varchar 네임스페이스에 정의된 sqltypes 유형을 사용하는 XML 스키마 컬렉션을 만듭니다.

CREATE XML SCHEMA COLLECTION SC AS '
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="myNS" xmlns:ns="myNS"
        xmlns:s="https://schemas.microsoft.com/sqlserver/2004/sqltypes">
   <import
     namespace="https://schemas.microsoft.com/sqlserver/2004/sqltypes"/>
      <simpleType name="myType">
            <restriction base="s:varchar">
                  <maxLength value="20"/>
            </restriction>
      </simpleType>
      <element name="root" type="ns:myType"/>
</schema>';
GO

다음과 같이 형식화된 XML 변수를 만들고, XML 인스턴스를 할당하고, 요소 형식 varchar<root> 값이 형식인지 확인할 수 있습니다.

DECLARE @var XML(SC);
SET @var = '<root xmlns="myNS">My data</root>'
SELECT @var.query('declare namespace sqltypes = "https://schemas.microsoft.com/sqlserver/2004/sqltypes";
declare namespace ns="myNS";
data(/ns:root[1]) instance of sqltypes:varchar?');
GO

요소 값은 instance of sqltypes:varchar? 변수와 연결된 스키마에 <root> 따라 varchar에서 파생된 형식이므로 식은 TRUE를 @var 반환합니다.

참고 항목