43
Polish SQL Server User Group Querying and Managing XML Data CHAPTER 7

SQL Server - Querying and Managing XML Data

Embed Size (px)

Citation preview

Page 1: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Querying and Managing XML Data

CHAPTER 7

Page 2: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

About the Author

Marek Maśko• Principal Database Analyst at Sabre

• Working with SQL Server for ~7 years

• SQL DBA, Dev & Architect

• MCP since 2012

• Contact Information: Email: [email protected]

LinkedIn: https://pl.linkedin.com/in/marekmasko

Twitter: @MarekMasko

Page 3: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Agenda

• XML Introduction

• Returning Results As XML with FOR XML

• Querying XML Data with XQuery

• Using the XML Data Type

Page 4: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

XML INTRODUCTION

Page 5: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Basic Example

Page 6: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Terminology

• A tag is a markup construct that begins with < and ends with >. Tags come in three flavors:

start-tag, such as <section>;

end-tag, such as </section>;

empty-element tag, such as <line-break />.

Page 7: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Terminology

• An element is a logical document component that either begins with a start-tag and ends with a matching end-tag or consists only of an empty-element tag. The characters between the start-tagand end-tag, if any, are the element's content.

<greeting>Hello, world!</greeting>;

<line-break />.

Page 8: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Terminology

• An attribute is a markup construct consisting of a name–value pair that exists within a start-tag or empty-element tag.

<img src="madonna.jpg" alt="Madonna" />;

<step number="3">Connect A to B.</step>.

• An XML attribute can only have a single value and each attribute can appear at most once on each element.

Page 9: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Basic Example

Page 10: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

XML

• Well-formed

• Ordered

• Case-sensitive Unicode

• Character data section

<![CDATA[ … ]]>

• Prolog

<?xml version="1.0" encoding="ISO-8859-15"?>

• XML Document vs XML Fragments

Page 11: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

XML Namespace

Page 12: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

XML Schema

• Standard to describe the metadata of XML documents

• XML Schema Description (XSD) document

• Document validation

• Typed XML

Page 13: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

RETURNING RESULTS AS XML WITH FOR XML

Lesson 01

Page 14: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

FOR XML clause

• Four modes:

–RAW

–AUTO

–PATH

– EXPLICIT

Page 15: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

LESSON REVIEW

Page 16: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Question 1

• Which FOR XML options are valid? (Choose all that apply.)

A. FOR XML AUTO

B. FOR XML MANUAL

C. FOR XML DOCUMENT

D. FOR XML PATH

Page 17: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Question 2

• Which directive of the FOR XML clause should you use to produce element-centric XML?

A. ATTRIBUTES

B. ROOT

C. ELEMENTS

D. XMLSCHEMA

Page 18: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Question 3

• Which FOR XML options can you use to manually format the XML returned? (Choose all that apply.)

A. FOR XML AUTO

B. FOR XML EXPLICIT

C. FOR XML RAW

D. FOR XML PATH

Page 19: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

QUERYING XML DATA WITH XQUERY

Lesson 02

Page 20: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

XQuery

• The standard language for browsing XML instances and returning XML.

• XQuery is, like XML, case sensitive.

• XQuery returns sequences. Sequences can include atomic values or complex values (XML nodes).

• Every identifier in XQuery is a qualified name, or a QName. A QName consists of a local name and, optionally, a namespace prefix.

• The SQL Server database engine processes XQuery inside T-SQL statements through XML data type methods.

Page 21: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Sequences

Page 22: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

XQuery Functions

• Numeric functions: ceiling(), floor(), round()

• String functions: concat(), contains(), substring(), string-length(), lower-case(), upper-case()

• Boolean and Boolean constructor functions: not(), true(), false()

• Nodes functions: local-name(), namespace-uri()

• Aggregate functions: count(), min(), max(), avg(), sum()

• Data accessor functions: data(), string()

• SQL Server extension functions: sql:column(), sql:variable()

Page 23: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Navigation

• XPath expressions

• Every path consists of a sequence of steps

Node-name/child::element-name[@attribute-name=value]

• Step may consist of three parts:– Axis Specifies the direction of travel. In the example, the axis is child::,

which specifies child nodes of the node from the previous step.

– Node test Specifies the criterion for selecting nodes. In the example, element-name is the node test; it selects only nodes named element-name.

– Predicate Further narrows down the search. In the example, there is one predicate: [@attribute-name=value], which selects only nodes that have an attribute named attribute-name with value value, such as [@orderid=10952].

Page 24: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Axis

Page 25: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Node test

• A node test follows the axis you specify

• Possibilities:– Simple node name

– Wildcard (*) = any principal node

• Node type tests:– comment() Allows you to select comment nodes.

– node() True for any kind of node. Do not mix this with the asterisk (*) wildcard; * means any principal node, whereas node() means any node at all.

– processing-instruction() Allows you to retrieve a processing instruction node.

– text() Allows you to retrieve text nodes, or nodes without tags.

Page 26: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Predicates

• Numeric – select nodes by position:

– /x/y[1]

– (/x/y)[1]

• Boolean – select nodes for which the predicate evaluates to true

Page 27: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Value comparison operators

Page 28: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

IF..THEN..ELSE

if (<expression1>)

then

<expression2>

else

<expression3>

• Like T-SQL CASE expression

Page 29: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

FLWOR Expressions

• FOR - bind iterator variables to input sequences

• LET - assign a value to a variable for a specific iteration

• WHERE - filter the iteration

• ORDER BY - control the order based on atomic values

• RETURN - format the resulting XML

Page 30: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

LESSON REVIEW

Page 31: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Question 1

• Which of the following is not a FLWOR clause?

A. for

B. let

C. where

D. over

E. return

Page 32: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Question 2

• Which node type test can be used to retrieve all nodes of an XML instance?

A. Asterisk (*)

B. comment()

C. node()

D. text()

Page 33: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Question 3

• Which conditional expression is supported in XQuery?

A. IIF

B. if..then..else

C. CASE

D. switch

Page 34: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

USING THE XML DATA TYPE

Lesson 03

Page 35: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

XML Data Type Methods

• query() – support querying

• value() – support retrieving atomic values

• exist() – support checking existence

• modify() – support modifying sections within the XML data

• nodes() – support shredding XML data into multiple rows in a result set

Page 36: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Using the XML Data Type for Dynamic Schema

Page 37: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

XML Indexes

• XML can be up to 2GB of data

• Primary XML index – shredded persistedrepresentation of the XML values;

• Three types of Secondary XML indexes:

–PATH

–VALUE

–PROPERTY

Page 38: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

LESSON REVIEW

Page 39: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Question 1

• Which of the following is not an XML data type method?

A. merge()

B. nodes()

C. exist()

D. value()

Page 40: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Question 2

• What kind of XML indexes can you create? (Choose all that apply.)

A. PRIMARY

B. PATH

C. ATTRIBUTE

D. PRINCIPALNODES

Page 41: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

Question 3

• Which XML data type method do you use to shred XML data to tabular format?

A. modify()

B. nodes()

C. exist()

D. value()

Page 42: SQL Server - Querying and Managing XML Data

Polish SQL Server User Group

THANK YOU!