2.2 SQL Server 2005 的 XML 支援功能
Overview XML Enhancements in SQL Server 2005 The xml Data Type Using XQuery
Lesson: XML Enhancements in SQL Server 2005 Enhancements to the FOR XML Clause Enhancements to the OPENXML Function
Enhancements to the FOR XML Clause EnhancementDescription ELEMENTS directive in RAW mode RAW mode queries can return element-centric XML. Support for NULL values Element-centric results can include empty elements for null values. Inline XSD schemas You can generate inline XSD schemas. TYPE directive returns results as xml data type Results from FOR XML queries can be xml values, making nested queries possible. PATH mode You can use XPath-like expressions to define XML results. ROOT directive You can specify a root element for your results. Named Element You can specify a named element for RAW and PATH mode queries.
Practice: Using the FOR XML Clause In this practice, you will: Return elements in RAW mode Return NULL values Return an inline XSD schema Use the TYPE directive Use PATH mode Use the ROOT directive Return named elements in a RAW mode query
Enhancements to the OPENXML Function EnhancementDescription Documents can be xml data type values The sp_xml_preparedocument stored procedure accepts xml parameters. Support for xml data type in the WITH clause Columns of type xml can be returned in the WITH clause. Batch-level scoping Document handles are scoped at the batch level and are released when the batch is completed.
Practice: Using the OPENXML Function In this practice, you will execute the OPENXML function
Lesson: The xml Data Type Storing XML in the Database How to Use Untyped XML How to Manage XML Schemas How to Use Typed XML How to Manage XML Indexes
Storing XML in the Database Benefits: Single store for structured and semistructured data Define variable content in a relational model Choose the most appropriate data model Single store for structured and semistructured data Define variable content in a relational model Choose the most appropriate data model Functionality: XML Indexes XQuery-based data retrieval XQuery-based modifications XML Indexes XQuery-based data retrieval XQuery-based modifications XML schema support: Typed XML: Validated by a schema Untyped XML: Nonvalidated XML Typed XML: Validated by a schema Untyped XML: Nonvalidated XML
How to Use Typed XML Declare a typed column or variable CREATE TABLE HumanResources.EmployeeResume (EmployeeID int, Resume xml (cvSchemas)) CREATE TABLE HumanResources.EmployeeResume (EmployeeID int, Resume xml (cvSchemas)) INSERT INTO HumanResources.EmployeeResume VALUES (1, '... ') INSERT INTO HumanResources.EmployeeResume VALUES (1, '... ') Assign typed XML (must conform to schema) Use CONTENT or DOCUMENT to allow/disallow fragments CREATE TABLE HumanResources.EmployeeResume (EmployeeID int, Resume xml (DOCUMENT cvSchemas)) CREATE TABLE HumanResources.EmployeeResume (EmployeeID int, Resume xml (DOCUMENT cvSchemas))
Lesson: Using XQuery What Is XQuery? How to Query XML with xml Data Type Methods How to Modify XML with the modify Method How to Shred XML to Relational Format with the Nodes Method
What Is XQuery? Describe the XML nodes to be retrieved Syntax includes and extends XPath 2.0 Based on working draft of W3C XQuery 1.0 language Specification ( Consist of two main sections: Prolog – Namespace declare and schema imported Body – XQuery expression specify the data to be retrieved
Martin Gudgin 33 short Simon Horrell 40 short Mark Szolkowski 30 medium Sample input XML document; people.xml
XQuery FLWOR Expressions FOR LET (not supported by SQL Server 2005) WHERE ORDER BY RETURN Using XQuery with the XML data type Methods xml.exist xml.value xml.query xml.nodes xml.modify Updating the XML data type with XQuery DML
XQuery Expressions XQuery is a superset of XPath (: this is a valid XQuery :) /people/person[age > 30] (: so is this FLWOR expression :) for $p in /people/person where $p/age > 30 order by $p/age[1] return $p/name
Order by clause XQuery can easily sort by using order by similar to SQL ORDER BY clause must resolve to a scalar value nodesets not comparable
XQuery FLWOR Expressions FOR LET (not supported by SQL Server 2005) WHERE ORDER BY RETURN Using XQuery with the XML data type Methods xml.exist xml.value xml.query xml.nodes xml.modify Updating the XML data type with XQuery DML
SQL Server 2005 Support Method of XQuery XQuery is supported through methods on the XML type xml.exist - returns bool xml.value - returns scalar xml.query - returns XML data type instance xml.nodes - returns one column rowset w/XML column xml.modify - modifies an instance These methods can return columns in rowsets - when used with SQL SELECT variables
How to Query XML with xml Data Type Methods SELECT xmlCol.value( 'int') SELECT xmlCol.value( 'int') Use the value method
Using xml.exist -- pdoc must have a person element -- as a child of the people root CREATE TABLE xmltab( id INTEGER PRIMARY KEY, pdoc XML CHECK (pdoc.exist('/people/person')=1) ) -- ok insert xmltab values( 1, ' ') -- fails, no persons insert xmltab values( 2, ' ')
How to Modify XML with the modify Method 'insert element salesperson {"Bill"} as first into (/InvoiceList/Invoice)[1]') 'insert element salesperson {"Bill"} as first into (/InvoiceList/Invoice)[1]') SET xmlCol.modify( replace value of (/InvoiceList/Invoice/SalesPerson/text())[1] with "Ted"') SET xmlCol.modify( replace value of (/InvoiceList/Invoice/SalesPerson/text())[1] with "Ted"') 'delete (/InvoiceList/Invoice/SalesPerson)[1]') 'delete (/InvoiceList/Invoice/SalesPerson)[1]') Use the insert statement Use the replace statement Use the delete statement