예제: ELEMENT 지시어 및 엔터티 인코딩 지정Example: Specifying the ELEMENT Directive and Entity Encoding

이 예에서는 ELEMENTXML 지시어 간의 차이점을 보여 줍니다.This example illustrates the difference between the ELEMENT and XML directives. ELEMENT 지시어는 데이터를 엔터티화하지만 XML 지시어는 그렇지 않습니다.The ELEMENT directive entitizes the data, but the XML directive does not. <Summary> 요소는 쿼리에서 할당된 XML인 <Summary>This is summary description</Summary>입니다.The <Summary> element is assigned XML, <Summary>This is summary description</Summary>, in the query.

다음 쿼리를 살펴보십시오.Consider this query:

USE AdventureWorks2012;  
GO  
SELECT  1 as Tag,  
        0 as Parent,  
        ProductModelID  as [ProductModel!1!ProdModelID],  
        Name            as [ProductModel!1!Name],  
        NULL            as [Summary!2!SummaryDescription!ELEMENT]  
FROM    Production.ProductModel  
WHERE   ProductModelID=19  
UNION ALL  
SELECT  2 as Tag,  
        1 as Parent,  
        ProductModelID,  
        NULL,  
       '<Summary>This is summary description</Summary>'  
FROM   Production.ProductModel  
WHERE  ProductModelID=19  
FOR XML EXPLICIT  

다음은 결과입니다.This is the result. 요약 설명이 결과에 엔터티화되어 있습니다.The summary description is entitized in the result.

<ProductModel ProdModelID="19" Name="Mountain-100">  
  <Summary>  
    <SummaryDescription><Summary>This is summary description</Summary></SummaryDescription>  
  </Summary>  
</ProductModel>  

이제 XML 지시어 대신 열 이름에 Summary!2!SummaryDescription!XMLELEMENT 지시어인 을 지정하면 엔터티화 없이 요약 설명을 받게 됩니다.Now, if you specify the XML directive in the column name, Summary!2!SummaryDescription!XML, instead of the ELEMENT directive, you will receive the summary description without entitization.

<ProductModel ProdModelID="19" Name="Mountain-100">  
  <Summary>  
    <SummaryDescription>  
      <Summary>This is summary description</Summary>  
    </SummaryDescription>  
  </Summary>  
</ProductModel>  

다음 쿼리에서는 정적 XML 값을 할당하는 대신 xml 유형의 query() 메서드를 사용하여 xml 유형의 CatalogDescription 열에서 제품 모델의 요약 설명을 검색합니다.Instead of assigning a static XML value, the following query uses the query() method of the xml type to retrieve the product model summary description from the CatalogDescription column of xml type. 결과는 xml 유형으로 알려져 있기 때문에 엔터티화가 적용되지 않습니다.Because the result is known to be of xml type, no entitization is applied.

SELECT  1 as Tag,  
        0 as Parent,  
        ProductModelID  as [ProductModel!1!ProdModelID],  
        Name            as [ProductModel!1!Name],  
        NULL            as [Summary!2!SummaryDescription]  
FROM    Production.ProductModel  
WHERE   CatalogDescription is not null  
UNION ALL  
SELECT  2 as Tag,  
        1 as Parent,  
        ProductModelID,  
        Name,  
       (SELECT CatalogDescription.query('  
            declare namespace pd="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";  
          /pd:ProductDescription/pd:Summary'))  
FROM     Production.ProductModel  
WHERE    CatalogDescription is not null  
ORDER BY [ProductModel!1!ProdModelID],Tag  
FOR XML EXPLICIT  

참고 항목See Also

FOR XML에서 EXPLICIT 모드 사용Use EXPLICIT Mode with FOR XML