This paper is based on the implementation of XQuery 1.0 in SQL Server 2005, which in turn is basedon the XQuery 1.0 July 2004 working draft. The first section of this paper provides an overview of the new XML data type and its associated features. Subsequent sections introduce the new XQuerylanguage and its advantages, the FLWOR statement, various operators in XQuery, built-in functionsin XQuery, type-related expressions, and non-supported features in SQL Server 2005, respectively.Finally, the last sections provide information on best practices and guidelines, XML datamodification, and XQuery usage scenarios.
The XML Data Type in SQL Server 2005
The new XML data type introduced in SQL Server 2005 provides users with the ability to store XMLdocuments and fragments in the database. An XML data type can be used to create a column, aparameter to a stored procedure or function, or a variable.Additionally, the user can associate a column of type XML with an XML schema collection to create atyped XML column. The XML schemas in the collection are used to validate and type the XMLinstances.
Typed vs. Untyped XML Data Type
An XML data type can be associated with an XML schema collection to have schema constraintsenforced on XML instances. If the XML data is associated with an XML schema collection, it is called
. Otherwise, it is called
.The SQL Server 2005 XML data type implements the ISO SQL-2003 standard XML data type. It canstore not only well-formed XML 1.0 documents, but also so-called XML content fragments with top-level text nodes and an arbitrary number of top-level elements. Checks for well-formedness of thedata are performed, and these checks do not require the XML data type to be bound to XMLschemas. Data that is not well-formed (subject to the SQL-2003 content relaxations) is rejected.Untyped XML is useful when the schema is not known
. It is also useful when the schema isknown but changing rapidly and thus hard to maintain, or multiple schemas exist and are latebound to the data based on external requirements. Furthermore, untyped XML is useful when theXML schemas contain XML Schema constructs not supported by the database engine (e.g.key/keyref, lax validation). In that case, you could use the System.XML validator inside a CLR(common language runtime) user-defined function to provide validation.If you have XML schemas in an XML schema collection describing your XML data, you can associatethe XML schema collection with the XML column to yield typed XML. The XML schemas are used tovalidate the data, perform more precise type checks than untyped XML during compilation of queryand data modification statements, and optimize storage and query processing.Typed-XML columns, parameters, and variables can store XML documents or content fragments,which you can specify as an option (DOCUMENT or CONTENT, respectively, with CONTENT as thedefault) at the time of declaration. Furthermore, you have to provide the collection of XML schemas.Specify DOCUMENT if each XML instance has exactly one top-level element; otherwise, use