sofia, bulgaria | 9-10 october using xquery to query and manipulate xml data stephen forte cto,...

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

Upload: randall-collins

Post on 29-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

Sofia, Bulgaria | 9-10 October

Using XQuery to Query and Manipulate XML DataUsing 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)

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

Sofia, Bulgaria | 9-10 October

AgendaAgenda

● XML in the Database?

● What is XQuery?

● Building XQuery Expressions

● XML DML

● XML in the Database?

● What is XQuery?

● Building XQuery Expressions

● XML DML

Sofia, Bulgaria | 9-10 October

XML or Relational?XML or Relational?

Data Characteristics

XML Relational

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)

Sofia, Bulgaria | 9-10 October

XML in a relational database??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

Sofia, Bulgaria | 9-10 October

XML and Relational!XML and Relational!Scenarios XML Relational

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

Sofia, Bulgaria | 9-10 October

XML DatatypeXML 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

Sofia, Bulgaria | 9-10 October

XML Schema SupportXML 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

Sofia, Bulgaria | 9-10 October

XML indexesXML 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)GOCREATE PRIMARY XML INDEX xml_idx on xml_tab (doc)GO

Sofia, Bulgaria | 9-10 October

AgendaAgenda

● XML in the Database?

● What is XQuery?

● Building XQuery Expressions

● XML DML

● XML in the Database?

● What is XQuery?

● Building XQuery Expressions

● XML DML

Sofia, Bulgaria | 9-10 October

What is XQueryWhat 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

Sofia, Bulgaria | 9-10 October

FLWOR ExpressionsFLWOR 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

Sofia, Bulgaria | 9-10 October

XQuery (FLWOR) ExpressionsXQuery (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/studentwhere $p/age > 65order by $p/age[1]return $p/name

Sofia, Bulgaria | 9-10 October

AgendaAgenda

● XML in the Database?

● What is XQuery?

● Building XQuery Expressions

● XML DML

● XML in the Database?

● What is XQuery?

● Building XQuery Expressions

● XML DML

Sofia, Bulgaria | 9-10 October

SQL Server XQuery FunctionalitySQL 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

Sofia, Bulgaria | 9-10 October

XQuery methodsXQuery 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

Sofia, Bulgaria | 9-10 October

exist()exist()value()value()query()query()

Sofia, Bulgaria | 9-10 October

XQuery: nodes()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

Sofia, Bulgaria | 9-10 October

sql:column()/sql:variable()sql:column()/sql:variable()

● 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=42select * 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.keywhere 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=42select * 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.keywhere T.x.exist(‘/a/b[@id=sql:column(“S.val”)]’)=1

Sofia, Bulgaria | 9-10 October

sql:variable, sql:columnsql:variable, sql:column

Sofia, Bulgaria | 9-10 October

AgendaAgenda

● XML in the Database?

● What is XQuery?

● Building XQuery Expressions

● XML DML

● XML in the Database?

● What is XQuery?

● Building XQuery Expressions

● XML DML

Sofia, Bulgaria | 9-10 October

XQuery: modify()XQuery: modify()

● Used with SET:

declare @xdoc xmlset @xdoc.modify(‘delete /a/b[@id=“42”]’)

update T set T.xdoc.modify(‘insert <b/> into /a’)where T.id=1Relational row-level concurrency: whole XML instance is locked

● Used with SET:

declare @xdoc xmlset @xdoc.modify(‘delete /a/b[@id=“42”]’)

update T set T.xdoc.modify(‘insert <b/> into /a’)where T.id=1Relational row-level concurrency: whole XML instance is locked

Sofia, Bulgaria | 9-10 October

xml.modifyxml.modify

Sofia, Bulgaria | 9-10 October

XML Indices: ReviewXML Indices: ReviewPRIMARY 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

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

Sofia, Bulgaria | 9-10 October

Session Summary 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

Sofia, Bulgaria | 9-10 October

Questions?Questions?

Sofia, Bulgaria | 9-10 October

Thanks!Thanks!

● Please fill out your evaluation form!

[email protected]

● Please put (XQuery in the subject)

● Please fill out your evaluation form!

[email protected]

● Please put (XQuery in the subject)

Sofia, Bulgaria | 9-10 October

ResourcesResources

SQL Server webpage: http://msdn.microsoft.com/SQL/2005/XML and Databases whitepapers: http://msdn.microsoft.com/XML/BuildingXML/XMLandDatabase/ Online WebCasts: http://msdn.microsoft.com/sql/2005/2005webcasts/ Newsgroups & Forum: microsoft.private.sqlserver2005.xmlhttp://communities.microsoft.com/newsgroups/default.asp?ICP=sqlserver2005&sLCID=us

http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=89