Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML...

27
Module 3: Using XML

description

Lesson 1: Retrieving XML by Using FOR XML Introduction to the FOR XML Clause What Are RAW Mode Queries? What Are AUTO Mode Queries? What Are EXPLICIT Mode Queries? What Are PATH Mode Queries? Syntax for Retrieving Nested XML Practice: Using FOR XML

Transcript of Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML...

Page 1: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Module 3:Using XML

Page 2: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Overview

Retrieving XML by Using FOR XML Shredding XML by Using OPENXMLIntroducing XQueryUsing the xml Data Type

Page 3: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Lesson 1: Retrieving XML by Using FOR XML

Introduction to the FOR XML Clause What Are RAW Mode Queries?What Are AUTO Mode Queries?What Are EXPLICIT Mode Queries?What Are PATH Mode Queries?Syntax for Retrieving Nested XMLPractice: Using FOR XML

Page 4: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Introduction to the FOR XML Clause

Extends SELECT syntaxReturns XML instead of rows and columnsConfigurable to return attributes, elements, and schemaBenefits client applications that work with XML

Converted to XML

Client ApplicationDatabase Server

Page 5: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

<row CustID="1" CustomerType="S" SalesOrderID="43860"/><row CustID="1" CustomerType="S" SalesOrderID="44501"/>...

SELECT Cust.CustomerID CustID, CustomerType, SalesOrderIDFROM Customer Cust JOIN SalesOrderHeader [Order] ON Cust.CustomerID = [Order].CustomerIDORDER BY Cust.CustomerIDFOR XML RAW

SELECT Cust.CustomerID CustID, CustomerType, SalesOrderIDFROM Customer Cust JOIN SalesOrderHeader [Order] ON Cust.CustomerID = [Order].CustomerIDORDER BY Cust.CustomerIDFOR XML RAW, ELEMENTS

<row><CustID>1</CustID><CustomerType>S</CustomerType><SalesOrderID>43860</SalesOrderID>

</row>...

SELECT Cust.CustomerID CustID, CustomerType, SalesOrderIDFROM Customer Cust JOIN SalesOrderHeader [Order] ON Cust.CustomerID = [Order].CustomerIDORDER BY Cust.CustomerIDFOR XML RAW('Order'), ROOT('Orders')

<Orders><Order><CustID>1</CustID><CustomerType>S</...</Order>...

</Orders>

What Are RAW Mode Queries?

XML representation of a rowset

Contains either elements or attributes

Optional root element and row element name

Page 6: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

What Are AUTO Mode Queries?

XML representation of data entitiesNest data based on join precedenceCan use options such as ELEMENTS and ROOTSELECT Cust.CustomerID CustID, CustomerType, SalesOrderIDFROM Customer Cust JOIN SalesOrderHeader [Order] ON Cust.CustomerID = [Order].CustomerIDORDER BY Cust.CustomerIDFOR XML AUTO

<Cust CustID="1" CustomerType="S"><Order SalesOrderID="43860" /><Order SalesOrderID="44501" />...

</Cust><Cust CustID="2" CustomerType="S">...

Page 7: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

SELECT 1 AS Tag, NULL AS Parent, SalesOrderID AS [Invoice!1!InvoiceNo], OrderDate AS [Invoice!1!Date!Element]FROM SalesOrderHeader FOR XML EXPLICIT

What Are EXPLICIT Mode Queries?

<Invoice InvoiceNo="43659"><Date>2001-07-01T00:00:00</Date>

</Invoice><Invoice InvoiceNo="43660">...

Tabular representations of XML documents

Allow complete control of XML format

Element

Attribute

SELECT 1 AS Tag, NULL AS Parent, SalesOrderID AS [Invoice!1!InvoiceNo], OrderDate AS [Invoice!1!Date!Element]FROM SalesOrderHeader FOR XML EXPLICIT

<Invoice InvoiceNo="43659"><Date>2001-07-01T00:00:00</Date>

</Invoice><Invoice InvoiceNo="43660">...

Tag Parent Invoice!1!InvoiceNo Invoice!1!Date!Element1 NULL 43659 2001-07-01T00:00:00

1 NULL 43660 2001-07-02T00:00:00

Page 8: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

What Are PATH Mode Queries?

Use XPath to specify XML formatAllow creation of nested dataEasier to use than EXPLICIT modeSELECT EmployeeID "@EmpID", FirstName "EmpName/First", LastName "EmpName/Last"FROM Person.Contact INNER JOIN Employee

ON Person.Contact.ContactID = Employee.ContactIDFOR XML PATH

<row EmpID="1"><EmpName>

<First>Guy</First><Last>Gilbert</Last>

</EmpName></row>...

Page 9: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Syntax for Retrieving Nested XML

AUTO mode produces only attributes or elements

Use inner FOR XML with TYPE clause to return xml data type

Combine EXPLICIT mode with UNION ALL

<Cust CustomerID="1" CustomerType="S"><Order SalesOrderID="43860" Status="5"/><Order SalesOrderID="44501" Status="5"/>...

</Cust>

SELECT Cust.CustomerID, CustomerType, SalesOrderID, Status

FROM Customer Cust JOIN SalesOrderHeader [Order]ON Cust.CustomerID = [Order].CustomerID

ORDER BY Cust.CustomerIDFOR XML AUTO

SELECT Cust.CustomerID, CustomerType, SalesOrderID, Status

FROM Customer Cust JOIN SalesOrderHeader [Order]ON Cust.CustomerID = [Order].CustomerID

ORDER BY Cust.CustomerIDFOR XML AUTO, ELEMENTS

<Cust><CustomerID>1</CustomerID><CustomerType>S</CustomerType><Order>

<SalesOrderID>43860</SalesOrderID><Status>5</Status>...

SELECT Name CategoryName,(SELECT Name SubCategoryNameFROM ProductSubCategory SubCategoryWHERE SubCategory.ProductCategoryID =

Category.ProductCategoryIDFOR XML AUTO, TYPE, ELEMENTS)

FROM ProductCategory CategoryFOR XML AUTO

<Category CategoryName="Accessories"><SubCategory>

<SubCategoryName>Bike Racks</SubCategoryName></SubCategory>...

<Invoice InvoiceNo="43659"><Date>2001-07-01T00:00:00</Date><LineItem ProductID="709">Bike Socks, M</LineItem><LineItem ProductID="711">Helmet, Blue</LineItem>

</Invoice>...

SELECT 1 AS Tag, NULL AS Parent, ...FROM SalesOrderHeaderUNION ALLSELECT 2 AS Tag, 1 AS Parent, ...FROM SalesOrderDetail OD JOIN ...FOR XML EXPLICIT

Page 10: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Practice: Using FOR XML

In this practice, you will:Retrieve XML in RAW modeRetrieve XML in AUTO modeRetrieve XML in EXPLICIT modeRetrieve XML in PATH mode

Page 11: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Lesson 2: Shredding XML by Using OPENXML

Overview of Shredding XML Data Stored Procedures for Managing In-Memory Node Trees OPENXML Syntax Syntax for Working With XML Namespaces Practice: Using OPENXML to Shred XML

Page 12: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

XML documentreceived from client

1

Overview of Shredding XML Data

Use OPENXML to retrieve rowset3

Use sp_xml_removedocumentto clean up memory tree5

Create internal tree representation by using sp_xml_preparedocument

2

Process (or shred) the data into tables

4

Page 13: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Stored Procedures for Managing In-Memory Node Trees

Create tree by using sp_xml_preparedocumentFree memory by using sp_xml_removedocument

CREATE PROC ProcessOrder @doc xml -- xml data

AS

-- Declare document handleDECLARE @hdoc integer

-- Create memory treeEXEC sp_xml_preparedocument @hdoc OUTPUT, @doc

-- Process Document

-- Remove memory treeEXEC sp_xml_removedocument @hdoc

Page 14: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

OPENXML Syntax

<Customer CustomerID="1" CustomerType="S"> <Order SalesOrderID="43860" Status="5" OrderDate="2001-08-01T00:00:00"> <OrderDetail ProductID="761" Quantity="2"/> <OrderDetail ProductID="770" Quantity="1"/> </Order></Customer>

SELECT *FROM OPENXML (@idoc, '/Customer/Order/OrderDetail', 1)WITH (CustomerID int '../../@CustomerID', OrderID int '../@SalesOrderID', OrderDate datetime '../@OrderDate', ProdID int '@ProductID', Quantity int) From Order

element

From Customer element

Uses attributes as defaultrowpattern identifies node level

Defaults to Quantity attribute From OrderDetail element

Page 15: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Syntax for Working With XML Namespaces

sp_xml_preparedocument accepts namespacesUse namespace prefix in all XPath expressions

<Customer xmlns="urn:AW_NS" xmlns:o="urn:AW_OrderNS" CustomerID="1" CustomerType="S">

<o:Order SalesOrderID="43860" Status="5" OrderDate="2001-08-01T00:00:00"> <o:OrderDetail ProductID="761" Quantity="2"/> <o:OrderDetail ProductID="770" Quantity="1"/> </o:Order></Customer>

EXEC sp_xml_preparedocument @idoc OUTPUT, @doc, <ROOT xmlns:rootNS="urn:AW_NS" xmlns:orderNS="urn:AW_OrderNS"/>'

SELECT * FROM OPENXML (@idoc, '/rootNS:Customer/orderNS:Order/orderNS:OrderDetail')WITH...

Page 16: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Practice: Using OPENXML to Shred XML

In this practice, you will:Use the OPENXML functionShred XML by using elements onlyShred XML by using attributes or elementsShred XML by using a colpattern parameter

Page 17: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Lesson 3: Introducing XQuery

What Is XQuery?XQuery BasicsXQuery Expressions

Page 18: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

What Is XQuery?

Query language to identify nodes in XML

/InvoiceList/Invoice[@InvoiceNo=1000]

FLWOR statements

Statement Descriptionfor Iterate through sibling nodes

where Apply filtering criteria to the iteration

order by Sort values in returned resultset

return Specify the XML to be returned

Page 19: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Sequences and QNames

XQuery Basics

Result of an XQuery expression is a sequenceAll identifiers are QNames

OperatorsArithmetic comparisonGeneral comparisonValue comparisonNode comparisonNode order comparisonLogical

Comments(: Comment text :)

if-then-elseif ( $A eq $B )then <result>A</result>else <result>B</result>

Page 20: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Path expressions

XQuery Expressions

Relative

Primary expressionsLiteralsVariable referencesFunction calls

Sequence expressionsConstruct, filter and combine sequences declare @x xml

set @x = '<root><abc></abc><abc attrAbc="1"></abc><abc attrAbc="2"></abc></root>'SELECT

@x.query('/root/abc[attrAbc]')

child::Address/child::Country

Absolute

/Address/Country

Page 21: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Lesson 4: Using the xml Data Type

What Is the xml Data Type?The query, value, and exist MethodsThe modify Method The nodes MethodPractice: Using the xml Data Type

Page 22: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

What Is the xml Data Type?

Native data type for XML Internal storage structure for XML InfoSet Use for tables, variables, or parametersExposes methods to query and modify XML

-- usage within table definitionCREATE TABLE NewTable( Col1 int primary key,

Col2 xml )

-- usage as local variabledeclare @data xml

-- usage as parameter to stored procedureCREATE PROCEDURE SaveData(@doc xml) AS ...

Page 23: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

The query, value, and exist Methods

SELECT xmlCol.query( '<InvoiceNumbers> { for $i in /InvoiceList/Invoice return <InvoiceNo> {number($i/@InvoiceNo)} </InvoiceNo> } </InvoiceNumbers>')

SELECT xmlCol.value('(/InvoiceList/Invoice/@InvoiceNo)[1]', 'int')

SELECT xmlCol.exist('/InvoiceList/Invoice[@InvoiceNo=1000]')

SELECT Invoices.query( '<Store> {sql:column("StoreName")} </Store>')

Use query to return untyped XML

Use value to return a scalar value

Bind relational columns and variables

Use exist to check for the existence of a specified value

Page 24: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

The modify Method

SET @xmlDoc.modify( 'insert element salesperson {"Bill"} as first into (/InvoiceList/Invoice)[1]')

SET xmlCol.modify( replace value of(/InvoiceList/Invoice/SalesPerson/text())[1] with "Ted"')

SET @xmlDoc.modify( 'delete (/InvoiceList/Invoice/SalesPerson)[1]')

insert adds child nodes or siblings to an XML document

replace value of updates a node in the XML document

delete removes a node from the XML document

Page 25: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

The nodes Method

SELECT nCol.value('@ProductID', 'int') Product,

nCol.value('@Quantity', 'int') QtyFROM @xmlOrder.nodes('/Order/LineItem')AS nTable(nCol)

SELECT nCol.value('../@OrderID[1]', 'int') ID, nCol.value('@ProductID[1]', 'int') ProdFROM Orders CROSS APPLY OrderDoc.nodes('/Order/LineItem') AS nTable(nCol)

Shreds xml variables into relational data

Requires the APPLY operator with xml columns

Page 26: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Practice: Using the xml Data Type

In this practice, you will: Use the xml data type Use the query, value, and exist methods Bind relational columns Use the modify method to insert, update,

and delete XML Use the nodes method

Page 27: Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.

Lab: Working With XML

Exercise 1: Mapping Relational Data and XML

Exercise 2: Storing XML Natively in the Database

Exercise 3: Using XQuery With xml Methods