13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

denote <strong>in</strong>sertions, deletions, and updates. The mapp<strong>in</strong>g schema of the <strong>XML</strong> viewconta<strong>in</strong>s the necessary <strong>in</strong><strong>for</strong>mation to map <strong>XML</strong> elements and attributes to tables andcolumns <strong>in</strong> the database. From a .<strong>NET</strong> Framework perspective, Updategrams look a lotlike DiffGrams. In SQL Server 2000, however, Updategrams are the native <strong>XML</strong>language to denote database changes.The OPEN<strong>XML</strong> Rowset ProviderOPEN<strong>XML</strong> is a T-SQL function that takes care of <strong>in</strong>sert<strong>in</strong>g data represented as an <strong>XML</strong>document. OPEN<strong>XML</strong> parses the contents of the <strong>XML</strong> document and exposes it as arowset. As a result, the records <strong>in</strong> the rowset can be stored <strong>in</strong> database tables.OPEN<strong>XML</strong> is not a write-only keyword that you can use only with INSERT or UPDATE.Because it is a generic rowset provider, you can use it with statements such asSELECT and SELECT INTO, and <strong>in</strong> general wherever a source table or view isaccepted.OPEN<strong>XML</strong> takes up to three arguments, as shown here:OPEN<strong>XML</strong> (handle, rowpattern [, flags])[WITH (SchemaDeclaration | TableName)]The first argument (handle) is the handle of the <strong>in</strong>ternal representation of an <strong>XML</strong>document. The document handle is created by the sp_xml_preparedocument systemstored procedure. The rowpattern argument is the XPath expression that selects thenodes <strong>in</strong> the source <strong>XML</strong> that must be processed as database rows.The flags argument is optional and, if specified, <strong>in</strong>dicates how attributes and elements<strong>in</strong> the selected nodes should be processed. By default, the flag is set to 1, which<strong>in</strong>dicates attribute-centric mapp<strong>in</strong>g. Attribute-centric mapp<strong>in</strong>g accepts <strong>in</strong>put values onlyfrom the attributes of the selected nodes. The mapp<strong>in</strong>g between attributes and columnsis determ<strong>in</strong>ed by name. Alternatively, you can specify element-centric mapp<strong>in</strong>g(a valueof 2). Element-centric mapp<strong>in</strong>g is similar to attribute-centric mapp<strong>in</strong>g except <strong>for</strong> the factthat it accepts <strong>in</strong>put values from the text of child element nodes.Caution You could also opt <strong>for</strong> mixed mapp<strong>in</strong>g—a value of 3—bycomb<strong>in</strong><strong>in</strong>g attribute-centric and element-centric mapp<strong>in</strong>g. In thiscase, attribute-centric mapp<strong>in</strong>g is applied first, and then <strong>for</strong> allstill unmatched columns, an element-centric mapp<strong>in</strong>g is applied.You should use this feature only when absolutely necessary.Us<strong>in</strong>g a double flag can significantly slow per<strong>for</strong>mance.The WITH clause is optional and can be used to def<strong>in</strong>e the schema of the target table.If a table with the desired schema already exists, you simply <strong>in</strong>dicate the table name.This is what commonly happens when you use OPEN<strong>XML</strong> to write data. When you useOPEN<strong>XML</strong> with a SELECT statement, you can specify the schema of the columnsbe<strong>in</strong>g returned. (More details on the syntax of OPEN<strong>XML</strong> can be found <strong>in</strong> SQL Server2000 Books Onl<strong>in</strong>e.)OPEN<strong>XML</strong> <strong>in</strong> ActionThe first step <strong>in</strong> us<strong>in</strong>g OPEN<strong>XML</strong> is call<strong>in</strong>g the sp_xml_preparedocument storedprocedure to parse the <strong>XML</strong> document. The stored procedure returns a treerepresentation of the nodes <strong>in</strong> the <strong>XML</strong> document, and this <strong>in</strong>-memory image becomesthe <strong>in</strong>put <strong>for</strong> OPEN<strong>XML</strong>. The stored procedure returns the handle of the document asan output parameter. Here's an example of how to use OPEN<strong>XML</strong>:DECLARE @handle <strong>in</strong>tEXEC sp_xml_preparedocument @handle OUTPUT,N'306

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!