예제: ELEMENT 지시어 지정Example: Specifying the ELEMENT Directive

이 예에서는 직원 정보를 검색하고 다음과 같이 요소 중심 XML을 생성합니다.This retrieves employee information and generates element-centric XML as shown in the following:

<Employee EmpID=...>  
  <Name>  
    <FName>...</FName>  
    <LName>...</LName>  
  </Name>  
</Employee>  

열 이름에 ELEMENT 지시어가 추가된 것을 제외하면 쿼리는 같은 상태로 유지됩니다.The query remains the same, except you add the ELEMENT directive in the column names. 따라서 특성 대신 <FName> 및 <LName> 요소 자식이 <Name> 요소에 추가됩니다.Therefore, instead of attributes, the <FName> and <LName> element children are added to the <Name> element. Employee!1!EmpID 열은 ELEMENT 지시어를 지정하지 않기 때문에 EmpID가 <Employee> 요소의 특성으로 추가됩니다.Because the Employee!1!EmpID column does not specify the ELEMENT directive, EmpID is added as the attribute of the <Employee> element.

SELECT 1    as Tag,  
       NULL as Parent,  
       E.BusinessEntityID as [Employee!1!EmpID],  
       NULL       as [Name!2!FName!ELEMENT],  
       NULL       as [Name!2!LName!ELEMENT]  
FROM   HumanResources.Employee AS E  
INNER JOIN Person.Person AS P  
ON  E.BusinessEntityID = P.BusinessEntityID  
UNION ALL  
SELECT 2 as Tag,  
       1 as Parent,  
       E.BusinessEntityID,  
       FirstName,   
       LastName   
FROM   HumanResources.Employee AS E  
INNER JOIN Person.Person AS P  
ON  E.BusinessEntityID = P.BusinessEntityID  
ORDER BY [Employee!1!EmpID],[Name!2!FName!ELEMENT]  
FOR XML EXPLICIT;  

다음은 결과의 일부입니다.This is the partial result.

<Employee EmpID="1">

<Name>

<FName>Ken</FName>

<LName>Sánchez</LName>

</Name>

</Employee>

<Employee EmpID="2">

<Name>

<FName>Terri</FName>

<LName>Duffy</LName>

</Name>

</Employee>

...

참고 항목See Also

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