Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sofia, Bulgaria | 9-10 October Using XQuery to Query and Manipulate XML Data Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen.

Similar presentations


Presentation on theme: "Sofia, Bulgaria | 9-10 October Using XQuery to Query and Manipulate XML Data Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen."— Presentation transcript:

1 Sofia, Bulgaria | 9-10 October Using XQuery to Query and Manipulate XML Data Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA)

2 Sofia, Bulgaria | 9-10 October Speaker.Bio.ToString() ●CTO and co-Founder of Corzen, Inc ●Microsoft RD, MVP and INETA Speaker ●International Conference Speaker for 9+ Years ●Wrote a few books on databases and development ● SQL Server 2005 Developers Guide (MS Press) ●Co-moderator & founder of NYC.NET Developers Group ●http://www.nycdotnetdev.com ●Former CTO of Zagat Survey ●CTO and co-Founder of Corzen, Inc ●Microsoft RD, MVP and INETA Speaker ●International Conference Speaker for 9+ Years ●Wrote a few books on databases and development ● SQL Server 2005 Developers Guide (MS Press) ●Co-moderator & founder of NYC.NET Developers Group ●http://www.nycdotnetdev.com ●Former CTO of Zagat Survey

3 Sofia, Bulgaria | 9-10 October Agenda ●XML in the Database? ●What is XQuery? ●Building XQuery Expressions ●XML DML ●XML in the Database? ●What is XQuery? ●Building XQuery Expressions ●XML DML

4 Sofia, Bulgaria | 9-10 October XML or Relational? Data Characteristics XMLRelational Flat Structured Data Hierarchical Structured Data Not First Class: PK-FK with cascading delete Semi-structured Data Not First Class Mark-up Data Not First Class: FTS Order preservation Not First Class Recursion (Recursive query)

5 Sofia, Bulgaria | 9-10 October XML in a relational database?? ●Applications may need to work with XML data, if so how do store it, you can: ●stored XML as text ●loses much of value of XML representation ●decompose XML into multiple relational tables ●allows use of relational technologies ●Store XML as an xml data type (New!) ●allows use of XML technologies ●Applications may need to work with XML data, if so how do store it, you can: ●stored XML as text ●loses much of value of XML representation ●decompose XML into multiple relational tables ●allows use of relational technologies ●Store XML as an xml data type (New!) ●allows use of XML technologies

6 Sofia, Bulgaria | 9-10 October XML and Relational! ScenariosXMLRelational Relational Data Exchange Use as transport, shred to relational Storage and Query Document Management Use as markup, store natively Provides framework to manage collections and relationships; provides Full-text search Semi-structured Data Represent semi- structured parts Represent structured parts Message audit Store natively Used for querying over promoted properties Object serialization Store natively Used for querying over promoted properties

7 Sofia, Bulgaria | 9-10 October XML Datatype ●Native SQL type ●Use for column, variable or parameter ●Can represent: ●XML 1.0 documents ●XML 1.0 fragments (0 to n element nodes and text nodes at top) ●Can be constrained by XML Schema collection ●Queryable with XQuery ●Updateable with XML-DML ●XML Indexing ●Well-formed and validation checks ●Native SQL type ●Use for column, variable or parameter ●Can represent: ●XML 1.0 documents ●XML 1.0 fragments (0 to n element nodes and text nodes at top) ●Can be constrained by XML Schema collection ●Queryable with XQuery ●Updateable with XML-DML ●XML Indexing ●Well-formed and validation checks

8 Sofia, Bulgaria | 9-10 October XML Schema Support ●XML Schema (World Wide Web Consortium [W3C] standard) ●Rich mechanism for type definitions and validation constraints ●Can be used to constrain XML documents ●Benefits of typed data ●Guarantees shape of data ●Allows storage and query optimizations ●XML type system ●Store XML schemas in system metadata ●Does not preserve annotations ●XML Schema (World Wide Web Consortium [W3C] standard) ●Rich mechanism for type definitions and validation constraints ●Can be used to constrain XML documents ●Benefits of typed data ●Guarantees shape of data ●Allows storage and query optimizations ●XML type system ●Store XML schemas in system metadata ●Does not preserve annotations

9 Sofia, Bulgaria | 9-10 October XML indexes ●You can create XML INDEXes on an XML column ●optimizes XML Queries on the column ●table or view must have clustered primary key ●composite XML index not allowed ●primary XML index must be created first ●three specialized index types also available ●VALUE, PATH, PROPERTY ●You can create XML INDEXes on an XML column ●optimizes XML Queries on the column ●table or view must have clustered primary key ●composite XML index not allowed ●primary XML index must be created first ●three specialized index types also available ●VALUE, PATH, PROPERTY CREATE TABLE xml_tab ( id integer primary key, doc xml) GO CREATE PRIMARY XML INDEX xml_idx on xml_tab (doc) GO

10 Sofia, Bulgaria | 9-10 October Agenda ●XML in the Database? ●What is XQuery? ●Building XQuery Expressions ●XML DML ●XML in the Database? ●What is XQuery? ●Building XQuery Expressions ●XML DML

11 Sofia, Bulgaria | 9-10 October What is XQuery ●XQuery is a language used to query and process XML data ●W3C Standard: ●http://www.w3.org/TR/xquery/ ●XQuery is a superset of XPath 2.0 ●XQuery uses expressions to query data- similar to SQL ●XQuery is a language used to query and process XML data ●W3C Standard: ●http://www.w3.org/TR/xquery/ ●XQuery is a superset of XPath 2.0 ●XQuery uses expressions to query data- similar to SQL

12 Sofia, Bulgaria | 9-10 October FLWOR Expressions ●XML Queries are FLWOR expressions ●made up of five types of clause ●FOR ●LET (not supported by SQL Server 2005 or System.Xml) ●WHERE ●ORDER BY ●RETURN ●XML Queries are FLWOR expressions ●made up of five types of clause ●FOR ●LET (not supported by SQL Server 2005 or System.Xml) ●WHERE ●ORDER BY ●RETURN

13 Sofia, Bulgaria | 9-10 October XQuery (FLWOR) Expressions ●XQuery is a superset of XPath ●can use XPath or FLWOR expressions ●Can also use both in one expression ●XPath used to select values in FLWOR expressions ●XQuery is a superset of XPath ●can use XPath or FLWOR expressions ●Can also use both in one expression ●XPath used to select values in FLWOR expressions for $p in /students/student where $p/age > 65 order by $p/age[1] return $p/name

14 Sofia, Bulgaria | 9-10 October Agenda ●XML in the Database? ●What is XQuery? ●Building XQuery Expressions ●XML DML ●XML in the Database? ●What is XQuery? ●Building XQuery Expressions ●XML DML

15 Sofia, Bulgaria | 9-10 October SQL Server XQuery Functionality ●SQL Server exposes a subset of XQuery ●W3C XQuery Spec Alignment ●XPath predicate supported only at end of expression ●Can use XQuery as a constraint on a field ●only a subset of functions and operators supported ●SQL Server exposes a subset of XQuery ●W3C XQuery Spec Alignment ●XPath predicate supported only at end of expression ●Can use XQuery as a constraint on a field ●only a subset of functions and operators supported

16 Sofia, Bulgaria | 9-10 October XQuery methods ●exist() returns 1 if the XQuery expression returns at least one item, 0 otherwise ●value() extracts an XQuery value into the SQL value and type space ●Expression has to statically be a singleton ●String value of atomized XQuery item is cast to SQL type ●SQL type has to be SQL scalar type (no XML or CLR UDT) ●query() creates new, untyped XML data type instance ●exist() returns 1 if the XQuery expression returns at least one item, 0 otherwise ●value() extracts an XQuery value into the SQL value and type space ●Expression has to statically be a singleton ●String value of atomized XQuery item is cast to SQL type ●SQL type has to be SQL scalar type (no XML or CLR UDT) ●query() creates new, untyped XML data type instance

17 Sofia, Bulgaria | 9-10 October exist()value()query()

18 XQuery: nodes() ●Provides OpenXML-like functionality on XML data type column in SQL Server 2005 ●Returns a row per selected node ●Each row contains a special XML data type instance that: ●References one of the selected nodes ●Preserves the original structure and types ●Can only be used with the XQuery methods (not modify()), count(*), and IS (NOT) NULL ●Provides OpenXML-like functionality on XML data type column in SQL Server 2005 ●Returns a row per selected node ●Each row contains a special XML data type instance that: ●References one of the selected nodes ●Preserves the original structure and types ●Can only be used with the XQuery methods (not modify()), count(*), and IS (NOT) NULL

19 Sofia, Bulgaria | 9-10 October sql:column()/sql:varia ble() ●Map SQL value and type into XQuery values and types in context of XQuery or XML-DML ●sql:variable(): accesses a SQL variable/parameter declare @value int set @value=42 select * from T where T.x.exist(‘/a/b[@id=sql:variable(“@value”)]’)=1 ●sql:column(): accesses another column value tables: T(key int, x xml), S(key int, val int) select * from T join S on T.key=S.key where T.x.exist(‘/a/b[@id=sql:column(“S.val”)]’)=1 ●Map SQL value and type into XQuery values and types in context of XQuery or XML-DML ●sql:variable(): accesses a SQL variable/parameter declare @value int set @value=42 select * from T where T.x.exist(‘/a/b[@id=sql:variable(“@value”)]’)=1 ●sql:column(): accesses another column value tables: T(key int, x xml), S(key int, val int) select * from T join S on T.key=S.key where T.x.exist(‘/a/b[@id=sql:column(“S.val”)]’)=1

20 Sofia, Bulgaria | 9-10 October sql:variable, sql:column

21 Sofia, Bulgaria | 9-10 October Agenda ●XML in the Database? ●What is XQuery? ●Building XQuery Expressions ●XML DML ●XML in the Database? ●What is XQuery? ●Building XQuery Expressions ●XML DML

22 Sofia, Bulgaria | 9-10 October XQuery: modify() ●Used with SET: declare @xdoc xml set @xdoc.modify(‘delete /a/b[@id=“42”]’) update T set T.xdoc.modify(‘insert into /a’) where T.id=1 Relational row-level concurrency: whole XML instance is locked ●Used with SET: declare @xdoc xml set @xdoc.modify(‘delete /a/b[@id=“42”]’) update T set T.xdoc.modify(‘insert into /a’) where T.id=1 Relational row-level concurrency: whole XML instance is locked

23 Sofia, Bulgaria | 9-10 October xml.modify

24 XML Indices: Review PRIMARY XML Index – Use when you have to run a XQuery-link to PK FOR VALUE – Useful for queries where values are more selective than paths such as //*[.=“Steve Forte”] FOR PATH – Useful for Path expressions: avoids joins by mapping paths to hierarchical index (HID) numbers. Example: /person/address/zip FOR PROPERTY – Useful when optimizer chooses other index (for example, on relational column, or FT Index) in addition so row is already known

25 Sofia, Bulgaria | 9-10 October Session Summary ●If you need to store XML data use the native type ●SQL Server implements the W3C XQuery Spec ●SQL Server extends the spec with XML DML ●If you need to store XML data use the native type ●SQL Server implements the W3C XQuery Spec ●SQL Server extends the spec with XML DML

26 Sofia, Bulgaria | 9-10 October Questions?

27 Sofia, Bulgaria | 9-10 October Thanks! ●Please fill out your evaluation form! ●stevef@orcsweb.comstevef@orcsweb.com ●Please put (XQuery in the subject) ●Please fill out your evaluation form! ●stevef@orcsweb.comstevef@orcsweb.com ●Please put (XQuery in the subject)

28 Sofia, Bulgaria | 9-10 October Resources SQL Server webpage: http://msdn.microsoft.com/SQL/2005/ http://msdn.microsoft.com/SQL/2005/ XML and Databases whitepapers: http://msdn.microsoft.com/XML/BuildingXML/XMLandDatabase/ http://msdn.microsoft.com/XML/BuildingXML/XMLandDatabase/ Online WebCasts: http://msdn.microsoft.com/sql/2005/2005webcasts/ http://msdn.microsoft.com/sql/2005/2005webcasts/ Newsgroups & Forum: microsoft.private.sqlserver2005.xml http://communities.microsoft.com/newsgroups/default.asp? ICP=sqlserver2005&sLCID=us http://communities.microsoft.com/newsgroups/default.asp? ICP=sqlserver2005&sLCID=us http://forums.microsoft.com/msdn/ShowForum.aspx? ForumID=89


Download ppt "Sofia, Bulgaria | 9-10 October Using XQuery to Query and Manipulate XML Data Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen."

Similar presentations


Ads by Google