XML Schema Collection

SQL Server offers native storage of XML data using the xml data type. It is possible to associate XSD (XML schema definition language) schemas with either a variable or a column of xml type using an XML schema collection. According to the Microsoft Developer Network, “The XML schema collection stores the imported XML schemas and is then used to do the following:

  • Validate XML instances
  • Type the XML data as it is stored in the database.”

An XML schema collection works as a metadata entity such as a table in the database. XML schemas can be created, modified or deleted. Schemas listed in a Transact-SQL CREATE XML SCHEMA COLLECTION statement are automatically imported into a newly created XML schema collection object. Furthermore, you can import other schemas or schema components into an existing collection already in the database by using the Transact-SQL ALTER XML SCHEMA COLLECTION statement.

XML stored in a column or variable associated with a schema is called typed XML, because the schema offers the required data type information for the instance data. This type information is very useful in optimizing data storage. In addition, schemas are referred to by the query-processing engine for confirming types, modifying data and optimizing queries.

An associated XML schema collection also makes it possible for SQL Server to validate an XML instance. The Microsoft developer Network explains, “If the XML instance complies with the schema, the database allows the instance to be stored in the system with their type information. Otherwise, it rejects the instance. “

The intrinsic function XML_SCHEMA_NAMESPACE can also be used to fetch a schema collection stored in the database. In addition, the XML schema collection is a useful tool for typing XML variables, parameters, and columns.