Presentation is loading. Please wait.

Presentation is loading. Please wait.

XQuery Your gateway to manipulating XML in SQL Server 2005.

Similar presentations


Presentation on theme: "XQuery Your gateway to manipulating XML in SQL Server 2005."— Presentation transcript:

1 XQuery Your gateway to manipulating XML in SQL Server 2005

2 About the Speaker… Hal Hayes Hal.hayes@tmaresources.com Sr. Director, Software Engineering TMA Resources, Leader in Member-centric Software Microsoft MCP (just shy of my MCAD) Founder, CAPAREA (formerly CAVBUG) Learning Tree Instructor (SQL Server) gratuitous aircraft picture

3 Agenda XQuery Defined XQuery Defined Working with XML in SQL Server 2005 Working with XML in SQL Server 2005 Advanced Usage Advanced Usage Topic Maps Topic Maps Recommendations Recommendations Resources Resources

4 XML XML XML Schemas XML Schemas XPath and XSLT XPath and XSLT Working with XML in.NET Working with XML in.NET Working with XML in SQL Server Working with XML in SQL Server What you should know...

5 What is XQuery??? XML-based functional, declarative query language XML-based functional, declarative query language XQuery is SQL-like XQuery is SQL-like Fine-grained querying against native XML (hierarchical vs. flat table/record format for SQL) Fine-grained querying against native XML (hierarchical vs. flat table/record format for SQL) Syntax is straight-forward making it easy (??) to query your XML data Syntax is straight-forward making it easy (??) to query your XML data Standards Based. Currently a W3C Candidate Recommendation* Standards Based. Currently a W3C Candidate Recommendation* *W3 standardized language (http://www.w3.org/XML/Query)

6 XQuery in SQL Server 2005 A subset of full language A subset of full language Namespace aware Namespace aware Can work with or without a Schema (strong vs. weak-typed) Can work with or without a Schema (strong vs. weak-typed) One major difference…no “let” statement! One major difference…no “let” statement!

7 XQuery Basics FLOWR FLOWR XPath XPath Comments Comments

8 FLOWR Main construct of XQuery is the FLOWR expression For-Let-Where-Order-Return For-Let-Where-Order-Return equivalence to SELECT- FROM-HAVING-WHERE in SQL equivalence to SELECT- FROM-HAVING-WHERE in SQL Use the Return construct to shape your results Use the Return construct to shape your results { for $b in doc("bib.xml")/bib/book where $b/publisher = “Microsft" and $b/@year gt 1991 return { $b/title } }

9 FLOWR (continued) for clause provides a definition of a variable and binding of iterations across a range of sequence values (SQL-SELECT) for clause provides a definition of a variable and binding of iterations across a range of sequence values (SQL-SELECT) let clause allows association of a variable to a further ordered list of tuples (SQL-SET) let clause allows association of a variable to a further ordered list of tuples (SQL-SET) where clause is a filter of the current sequence or tuples from for/let (SQL-WHERE) where clause is a filter of the current sequence or tuples from for/let (SQL-WHERE) order clause sorts current results based on a given criteria (SQL- ORDER) order clause sorts current results based on a given criteria (SQL- ORDER) return clause is used to create output that can be XML or not XML (SQL-RETURN) return clause is used to create output that can be XML or not XML (SQL-RETURN)

10 Two Examples Standard Recommendation XQuery Standard Recommendation XQuery (using XMLSpy) SQL Server 2005 Version SQL Server 2005 Version

11 Additional XML Data Type Methods In SQL 2005 Column.query() Column.query() Column.value() Column.value() Column.modify() Column.modify() Column.nodes() Column.nodes() Column.exists() Column.exists()

12 XQuery DML Column.query(‘statement’) Column.query(‘statement’) Use XQuery statement, or Use XQuery statement, or XPath XPath Declaration of a namespace Declaration of a namespace Can return XML Can return XML Column.value(‘statement’, ‘type’) Column.value(‘statement’, ‘type’) Returns a value Returns a value Use SQL types (i.e. ‘varchar(255)’) Use SQL types (i.e. ‘varchar(255)’)

13 XQuery DML Column.nodes() Column.nodes() Useful for “shredding” an XML document into its constituent parts. Useful for “shredding” an XML document into its constituent parts. If used in the WHERE clause, can be used in conjunction with.query(),.exist(),.value(), and.nodes() (but not.modify()) If used in the WHERE clause, can be used in conjunction with.query(),.exist(),.value(), and.nodes() (but not.modify()) Column.exist() Column.exist() Returns a 1 (representing True) if the XQuery expression in a query returns a nonempty result (meaning it returns at least one XML node). Returns a 1 (representing True) if the XQuery expression in a query returns a nonempty result (meaning it returns at least one XML node). Returns a 0 (representing False) if the XQuery returns and empty result. Returns a 0 (representing False) if the XQuery returns and empty result.

14 XQuery DML Column.modify() Column.modify() Used for XML DML operations (non-query) Used for XML DML operations (non-query) ‘update’, ‘delete’, ‘replace value of ’ ‘update’, ‘delete’, ‘replace value of ’ Executed as part of the DML “Update” statement Executed as part of the DML “Update” statement UPDATE customerData SET customerDocs.modify(' insert no history available as first into (//customer)[1]')

15 Demos With some basic stuff With some basic stuff Maybe some advanced stuff, too! Maybe some advanced stuff, too!

16 Quick Introduction to Topic Maps Constructs for creating a meta-model of data and information Constructs for creating a meta-model of data and information Topic Maps are XML based Topic Maps are XML based Current (pending) standard is XTM 2.0 (ISO) Current (pending) standard is XTM 2.0 (ISO)

17 Topic Map Fundamentals “An Introduction to Topic Maps”, Kal Ahmed and Graham Moore, The Architectural Journal, http://www.architecturejournal.net/2005/issue5/Jour5Intro/

18 Topic Map Demo

19 Recommendations For getting at detail in your XML documents stored in the database, XQuery is a great tool. For getting at detail in your XML documents stored in the database, XQuery is a great tool. Don’t rely on the XQuery Where clause…you could retrieve empty records! Don’t rely on the XQuery Where clause…you could retrieve empty records! Strong-typed (mapped to Schema) XML Data Columns will have better performance than weak-typed. Strong-typed (mapped to Schema) XML Data Columns will have better performance than weak-typed. DML is limited. You have better choices for manipulation (i.e..NET XML, XLinq) DML is limited. You have better choices for manipulation (i.e..NET XML, XLinq) Dynamic Parameterized XQueries are problematic, but can be done. Best bet is to encase them in StoredProcs. Dynamic Parameterized XQueries are problematic, but can be done. Best bet is to encase them in StoredProcs.

20 Resources W3C XQuery Working Group W3C XQuery Working Group http://www.w3.org/XML/Query Topic Map ISO Topic Map ISO http://www.isotopicmaps.org/sam/sam-xtm / http://www.isotopicmaps.org/sam/sam-xtm / Topic Map Consortium Topic Map Consortium http://www.topicmaps.org

21 Books A Developer's Guide to SQL Server 2005, Beauchemin, Berglund, Sullivan; Addison-Wesley A Developer's Guide to SQL Server 2005, Beauchemin, Berglund, Sullivan; Addison-Wesley XQuery Kick Start, James McGovern, et al.; SAMS XQuery Kick Start, James McGovern, et al.; SAMS XQuery, The XML Query Language, Michael Brundage, Addison-Wesley XQuery, The XML Query Language, Michael Brundage, Addison-Wesley XQuery from the Experts, Don Chamberlin, et al., Addison-Wesley XQuery from the Experts, Don Chamberlin, et al., Addison-Wesley

22 Blogs of Note Michael Rys, Program Manager for SQL Server's XML Technologies and member of W3 XML Query Working Group http://sqljunkies.com/WebLog/mrys Kent Tegels, DevelopMentor, MVP http://sqljunkies.com/WebLog/ktegels Mike Champion, Program Manager for XML Standards XML WebData team at Microsoft http://blogs.msdn.com/mikechampion Shankar Pal, XML SQL Program Manager, Microsoft Corporation http://blogs.msdn.com/spal Microsoft Corporation XML Team Weblog http://blogs.msdn.com/xmlteam/default.aspx Bob Beauchemin's Blog, Author of A First Look at SQL Server 2005 for Developers http://staff.develop.com/bobb/weblog/default.aspx


Download ppt "XQuery Your gateway to manipulating XML in SQL Server 2005."

Similar presentations


Ads by Google