XML Data Type in SQL Server 2005




The SQL Server 2005 XML data type implements the ISO SQL-2003 standard XML data type. In an XML
data typed column, you can store both well-formed XML 1.0 documents as well as XML content fragments
with text nodes. Moreover you can also store an arbitrary number of top-level elements in an untyped
XML column. At the time of inserting the XML data, the system checks for the well-formedness of the
data and rejects data that is not well-formed. The extent of the server-side validation is based on whether
an XSD schema is associated with the XML data type column. Before looking at the XSD schemas and
their role with an XML column, you need to understand the reasons for storing native XML data in an
XML data type column. Storing XML data in an XML data type column can be extremely useful in the
following situations:

  By storing XML data in the SQL Server, you have a straightforward way of storing your XML

data at the server while preserving document order and document structure

  When you want the ability to query and modify your XML data

  When you want to exchange data with external systems without performing a lot of

transformations

  When you have XML documents with a wide range of structures, or XML documents conform-

ing to different or complex schemas that are too hard to map to relational structures

SQL Server 2005 stores XML data as Unicode (UTF-16). XML data retrieved from the
server comes out in UTF-16 encoding as well. If you want a different encoding, you
need to perform the necessary conversion after retrieving the data either by casting
or on the mid-tier. For example, you may cast your XML data to varchar type on the
server, in which case the database engine serializes the XML with an encoding deter-
mined by the collation of the varchar.

Typed versus Untyped XML Column

For more structure or validation of XML data, SQL Server lets you associate schema with a particular
XML column. This column is named typed XML column. If an XML schema is associated with an XML
column, the schema validates the XML data at the time of inserting the XML data into the field. SQL
Server 2005 supports many schemas grouped together in a schema collection, which lets you apply dif-
ferent schemas to an XML column. The server will validate all incoming XML against all the schemas. If
the XML is valid for any of the collection’s schemas, it can be stored in the XML field. Table 10-1 summa-
rizes the differences between a typed XML column and an untyped XML column.

Table 10-1. Differences between a Typed and an Untyped XML Column


learn guitarphysics learnteliphonyxmlphysicsenjoylife