XML Styles Supported by CONVERT() in SQL Server

Below is a list of XML styles supported by the CONVERT() function in SQL Server. These are the styles that can be used as the third argument to indicate how the input value is formatted when converting to XML.

ValueOutput
0 (default)Use default parsing behavior that discards insignificant white space, and doesn’t allow for an internal DTD subset.

Note: When converting to the xml data type, SQL Server insignificant white space is handled differently than in XML 1.0.
1Preserve insignificant white space. This style setting sets the default xml:space handling to match the behavior of xml:space="preserve".
2Enable limited internal DTD subset processing.

If enabled, the server can use the following information that is provided in an internal DTD subset, to perform nonvalidating parse operations.

– Defaults for attributes are applied
– Internal entity references are resolved and expanded
– The DTD content model is checked for syntactical correctness

The parser ignores external DTD subsets. Also, it doesn’t evaluate the XML declaration to see whether the standalone attribute has a yes or no value. Instead, it parses the XML instance as a stand-alone document.
3Preserve insignificant white space, and enable limited internal DTD subset processing.

Source: Microsoft

Example of Usage

Here’s a quick example of how to use the above styles:

SELECT CONVERT( xml, '<root> <child> Value </child> </root>', 3 );

Result:

<root> <child> Value </child> </root>

Here I provided a style of 3, which preserves insignificant white space, and enable limited internal DTD subset processing.

Here’s what it looks like with a style of 0, which removes insignificant white space:

SELECT CONVERT( xml, '<root> <child> Value </child> </root>', 0 );

Result:

<root><child> Value </child></root>

Insignificant white space has been removed.

We can use the sys.dm_exec_describe_first_result_set function to check the data type of the return value:

SELECT system_type_name
FROM sys.dm_exec_describe_first_result_set(
    'SELECT CONVERT( xml, ''<root> <child> Value </child> </root>'', 0 );', 
    null, 
    0);

Result:

xml