XML and Internet Support

Writing XML Using OPENXML

OPENXML is a Transact-SQL keyword that provides a rowset over in-memory XML documents. OPENXML is a rowset provider similar to a table or a view. OPENXML allows access to XML data as if it is a relational rowset by providing a rowset view of the internal representation of an XML document. The records in the rowset can be stored in database tables (similar to the rowsets provided by tables and views).

OPENXML can be used in SELECT, and SELECT INTO statements wherever rowset providers, such as a table, a view or OPENROWSET can appear as the source. For information about the syntax of OPENXML, see OPENXML.

To write queries against an XML document using OPENXML, you must first call sp_xml_preparedocument, which parses the XML document and returns a handle to the parsed document that is ready for consumption. The parsed document is a tree representation of various nodes (elements, attributes, text, comment, and so on) in the XML document. The document handle is passed to OPENXML, which then provides a rowset view of the document based on the parameters passed to it.

The internal representation of an XML document must be removed from memory by calling sp_xml_removedocument system stored procedure to free the memory.

This illustration shows the process.

Note  To understand OPENXML, familiarity with XPath queries and understanding of XML is required. For more information about XPath support in Microsoft® SQL Server™ 2000, see Using XPath Queries.

OPENXML Parameters

The parameters to OPENXML include:

XML Document Handle (idoc)

The document handle is returned by the sp_xml_preparedocument stored procedure.

XPath Expression to Identify the Nodes to Be Processed (rowpattern)

The XPath expression specified as rowpattern identifies a set of nodes in the XML document. Each node identified by rowpattern corresponds to a single row in the rowset generated by OPENXML.

The nodes identified by the XPath expression can be any XML node (elements, attributes, processing instructions, and so on) in the XML document. If rowpattern identifies a set of elements in the XML document, there is one row in the rowset for each element node identified. For example, if rowpattern ends in an attribute, a row is created for each attribute node selected by rowpattern.

Description of the Rowset to Be Generated

A rowset schema must be provided to OPENXML to generate the rowset. You can specify the rowset schema by using the optional WITH clause. These options are available for specifying the rowset schema:

Mapping Between the Rowset Columns and the XML Nodes

In the OPENXML statement, you can optionally specify the type of mapping (attribute-centric, element-centric) between the rowset columns and the XML nodes identified by the rowpattern. This information is used in transformation between the XML nodes and the rowset columns.

There are two ways to specify the mapping (you can specify both):

Both the flags and ColPattern parameters are optional. If no mapping is specified, attribute-centric mapping (default value of flags parameter) is assumed by default.

Attribute-centric Mapping

If the flags parameter in OPENXML is set to attributes map to the columns in the rowset based on the name correspondence. Name correspondence means that XML attributes of a given name are stored in a column in the rowset with the same name.

If the column name is different from the attribute name to which it maps, ColPattern must be specified.

If XML attribute has a namespace qualifier, the column name in the rowset must have the qualifier as well.

Element-centric Mapping

Setting the flags parameter in OPENXML to 2 (XML_ELEMENTS) specifies the element-centric mapping. It is similar to attribute-centric mapping except for these differences:

See Also

sp_xml_preparedocument

sp_xml_removedocument

OPENXML