--- title: "Example: Specifying the XMLTEXT Directive | Microsoft Docs" ms.custom: "" ms.date: "04/05/2017" ms.prod: "sql-server-2016" ms.reviewer: "" ms.suite: "" ms.technology: - "dbe-xml" ms.tgt_pltfrm: "" ms.topic: "article" helpviewer_keywords: - "XMLTEXT directive" ms.assetid: e78008ec-51e8-4fd1-b86f-1058a781de17 caps.latest.revision: 10 author: "BYHAM" ms.author: "rickbyh" manager: "jhubbard" --- # Example: Specifying the XMLTEXT Directive This example illustrates how data in the overflow column is addressed by using the **XMLTEXT** directive in a `SELECT` statement using EXPLICIT mode. Consider the `Person` table. This table has an `Overflow` column that stores the unconsumed part of the XML document. ``` USE tempdb; GO CREATE TABLE Person(PersonID varchar(5), PersonName varchar(20), Overflow nvarchar(200)); GO INSERT INTO Person VALUES ('P1','Joe',N'content') ,('P2','Joe',N'') ,('P3','Joe',N'content'); ``` This query retrieves columns from the `Person` table. For the `Overflow` column, *AttributeName* is not specified, but *directive* is set to `XMLTEXT` as part of providing a universal table column name. ``` SELECT 1 as Tag, NULL as parent, PersonID as [Parent!1!PersonID], PersonName as [Parent!1!PersonName], Overflow as [Parent!1!!XMLTEXT] -- No AttributeName; XMLTEXT directive FROM Person FOR XML EXPLICIT; ``` In the resulting XML document: - Because *AttributeName* is not specified for the `Overflow` column and the `xmltext` directive is specified, the attributes in the <`overflow`> element are appended to the attribute list of the enclosing <`Parent`> element. - Because the `PersonID`attribute in the <`xmltext`> element conflicts with the `PersonID` attribute retrieved on the same element level, the attribute in the <`xmltext`> element is ignored, even if `PersonID` is NULL. Generally, an attribute overrides an attribute of the same name in the overflow. This is the result: ``` content content ``` If the overflow data has subelements and the same query is specified, the subelements in the `Overflow` column are added as the subelements of the enclosing <`Parent`> element. For example, change the data in the `Person` table so that the `Overflow` column now has subelements. ``` USE tempdb; GO TRUNCATE TABLE Person; GO INSERT INTO Person VALUES ('P1','Joe',N'content') ,('P2','Joe',N'') ,('P3','Joe',N'PersonName'); ``` If the same query is executed, the subelements in the <`xmltext`> element are added as subelements of the enclosing <`Parent`> element: ``` SELECT 1 as Tag, NULL as parent, PersonID as [Parent!1!PersonID], PersonName as [Parent!1!PersonName], Overflow as [Parent!1!!XMLTEXT] -- no AttributeName, XMLTEXT directive FROM Person FOR XML EXPLICIT; ``` This is the result: ``` content PersonName ``` If *AttributeName* is specified with the `xmltext` directive, the attributes of the <`overflow`> element are added as attributes of the subelements of the enclosing <`Parent`> element. The name specified for *AttributeName* becomes the name of the subelement In this query, *AttributeName*, <`overflow`>, is specified together with the `xmltext` directive*:* ``` SELECT 1 as Tag, NULL as parent, PersonID as [Parent!1!PersonID], PersonName as [Parent!1!PersonName], Overflow as [Parent!1!overflow!XMLTEXT] -- Overflow is AttributeName -- XMLTEXT is a directive FROM Person FOR XML EXPLICIT ``` This is the result: ``` content PersonName ``` In this query element, *directive* is specified for `PersonName` attribute. This results in `PersonName` being added as a subelement of the enclosing <`Parent`> element. The attributes of the <`xmltext`> are still appended to the enclosing <`Parent`> element. The contents of the <`overflow`> element, subelements, are prepended to the other subelements of the enclosing <`Parent`> elements. ``` SELECT 1 AS Tag, NULL as parent, PersonID AS [Parent!1!PersonID], PersonName AS [Parent!1!PersonName!element], -- element directive Overflow AS [Parent!1!!XMLTEXT] FROM Person FOR XML EXPLICIT; ``` This is the result: ``` contentJoe Joe PersonName Joe ``` If the `XMLTEXT` column data contains attributes on the root element, these attributes are not shown in the XML data schema and the MSXML parser does not validate the resulting XML document fragment. For example: ``` SELECT 1 AS Tag, 0 ASParent, N'' AS 'overflow!1!!xmltext' FOR XML EXPLICIT, xmldata; ``` This is the result. Note that in the returned schema, the overflow attribute `a` is missing from the schema: ``` ` ` ` ``` ## See Also [Use EXPLICIT Mode with FOR XML](../../relational-databases/xml/use-explicit-mode-with-for-xml.md)