Columns with the Name of an XPath Node Test
If the column name is one of the XPath node tests, the content is mapped as shown in the following table.
When the column name is an XPath node test, the content is mapped to the corresponding node. If the SQL type of the column is xml, an error is returned.
For a column with the name of text(), the string value in that column is added as a text node.
For a column with the name of comment(), the string value in that column is added as an XML comment.
For a column with the name of node(), the result is the same as it is when the column name is a wildcard character (*).
For a column with the name of a processing instruction, the string value in that column is added as the PI value for the processing instruction target name.
The following query shows the use of the node tests as column names. It adds text nodes and comments in the resulting XML.
SELECT EmployeeID "@EmpID", 'Example of using node tests such as text(), comment(), processing-instruction()' as "comment()", 'Some PI' as "processing-instruction(PI)", 'Employee name and address data' as "text()", 'middle name is optional' as "EmpName/text()", FirstName as "EmpName/First", MiddleName as "EmpName/Middle", LastName as "EmpName/Last", AddressLine1 as "Address/AddrLine1", AddressLine2 as "Address/AddrLIne2", City as "Address/City" FROM HumanResources.EmployeeAddress E, Person.Contact C, Person.Address A WHERE E.EmployeeID = C.ContactID AND E.AddressID = A.AddressID AND E.EmployeeID=1 FOR XML PATH
This is the result:
<row EmpID="1"> <!--Example of using node tests such as text(), comment(), processing-instruction() --> <?PI Some PI?> Employee name and address data <EmpName>middle name is optional <First>Gustavo</First> <Last>Achong</Last> </EmpName> <Address> <AddrLine1>7726 Driftwood Drive</AddrLine1> <City>Monroe</City> </Address> </row>