Download DEVQ400-03 SQL Server 2000 XML Enhancements

Document related concepts

DBase wikipedia , lookup

Concurrency control wikipedia , lookup

Tandem Computers wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Oracle Database wikipedia , lookup

Microsoft Access wikipedia , lookup

Btrieve wikipedia , lookup

Team Foundation Server wikipedia , lookup

Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Relational model wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Database model wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Clusterpoint wikipedia , lookup

Transcript
SQL Server 2000
XML Enhancements
DEVQ400-03 Level 300
Henn Sarv
MCT MCDBA MCSD MCSE …
IT Koolitus
IT Grupp
What You Will See Today

XML Support in Microsoft SQL Server
2000




XML/XSLT fundamentals
Available features
Development techniques used to take
advantage of these features
SQL Server 2000 integration with other
tools through XML
Session Prerequisites





XML Basics
Transact-SQL (T-SQL) Language
Microsoft Visual Basic Language
(Basics)
Microsoft Visual Basic Development
Environment
SQL Server Tools
Agenda






XML Overview
XML in SQL Server 2000
Database Publishing
OpenXML Rowsets
SQL Server 2000 XML Solutions
BizTalk Server 2000 Integration (ei!)
XML Overview
Origin of XML

SGML Originated as a Markup
Language for Documents

Many other languages derive from it


XML, HTML, etc.
The Main Goal Is to Unify the Data
and Document Transport Format

XML is a W3C standard
XML Overview
Origin of XML

XML Originated as a Standard
Language for Data Representation
SGML
HTML
CSS
XML
XSLT
XML Overview
XML Basics

XML Structures the Content



In HTML: <p> Jan 15, 2000 </p>
In XML: <OrderDate> Jan 15, 2000
<OrderDate>
XML Does Not Display or Transform
Data

XML separates data from formatting and
transforming

HTML and XML are derived from SGML
XML Overview
XML Syntax

XML Is Composed of Tags and
Attributes

Tags can be nested

Representing entities, entity properties, and
entity hierarchy
<ROOT>
<Orders OrderID="10643" CustomerID="ALFKI"
EmployeeID="6" OrderDate="1997-0825T00:00:00" RequiredDate="1997-0922T00:00:00" ShippedDate="1997-0902T00:00:00" />
</ROOT>
XML Overview
Node Structure of XML Documents

XML Is Parsed into a Tree Structure

Nodes of the tree contain the data
Document
Elements
Text
...
Text
Text
}
Attributes
XML Overview
Node Structure of XML Documents

Parsing XML into Nodes
Namespace
uri=“…”
Comment
root
customer
customer list
id
customer
id
345
name
120
orders
first
last
Jane
Doe
order
date
07/16/98
order
date
07/23/98
XML Overview
XML Basics

Use XSLT to Display and Transform
Data

For example, XSLT can tell Internet
Explorer how to format each tag and
eventually how to transform it
<?xml-stylesheet type="text/xsl"
href="t12.xsl"?>
XML Overview
XSLT Basics


XSLT Rules Are Applied at the End of
the Process
Once the XML Document Is Parsed and
the DOM Is Instantiated with Document
Data, XSLT Transformations Are Applied
XML
DOM
XSLT
Final
Output
(HTML)
XML Overview
XSLT Basics

Example

This XSLT code first sets the table
formatting, then it defines the content of
each table cell
...
<TABLE STYLE="border:1px solid black">
<xsl:for-each select="ROOT/customers">
<TR >
<TD><xsl:value-of select="CustomerID"/></TD>
<TD ><xsl:value-of select="ContactName"/></TD>
<TD><xsl:value-of select="CompanyName"/></TD>
</TR>
</xsl:for-each>
</TABLE>
...
XML Overview
XSLT Basics
...
<TABLE STYLE="border:1px solid black">
<xsl:for-each select="ROOT/customers">
<TR >
<TD><xsl:value-of select="CustomerID"/></TD>
<TD ><xsl:value-of select="ContactName"/></TD>
<TD><xsl:value-of select="CompanyName"/></TD>
</TR>
</xsl:for-each>
</TABLE>
...
XML Overview
XSLT Basics

Three Basic Steps for XSLT
Transformation
Table X
#1: Draw an
empty table
X
X
X
X
X
#3: Populate each cell in
the HTML table with the
element’s text value
#2: Select all elements from
ROOT/Customers node
XML Overview
XPath Basics


An XPath Provides a Simple Mechanism
for Finding and Addressing Specific
Parts of an XML Document
The XPath Selects Element Nodes from
a Document


Specifies a path in the node tree
Filters nodes with a selection criteria
based on element and attribute values
XML Overview
XML Schemas

XML Schemas Describe the Structure of
an XML Document



XML schemas describe the tag and
attribute specifications
XML schemas also describe constraints on
the contained text
XML schemas and the DTD are mutually
exclusive
XML Overview
DOM Basics




Document Object Model
World Wide Web Consortium (W3C)
Language-Independent Interface
Provides Access to XML Structure
Through an Object-Oriented Model
Implemented in msxml.dll

Microsoft.XMLDOM
XML Overview
Microsoft DOM Implementation
XMLDOMDocument / XMLDOMNode
– XMLDOMNodeList
– XMLDOMNode
– XMLDOMNodeList
– XMLDOMAttribute
– XMLDOMElement
– XMLDOMNodeList
– XMLDOMNodeMap
– XMLDOMNamedNodeMap
– XMLDOMAttribute
XML Overview
Microsoft DOM Implementation

XMLDOM = XML Document Object
Model




XMLDOMDocument – top node in the tree
XMLDOMNode – represents a node in the
tree
XMLDOMNodeList – collection of nodes
XMLDOMNamedNodeMap – collection of
attribute nodes
XML Overview
Anatomy of an Element Node
parentNode
nodeType = Element
nodeName = customer
previousSibling nodeValue = null
hasChildNodes = true
firstChild
attributes
childNodes
NodeList
nextSibling
lastChild
...
NamedNodeMap
...
XML Overview
Using DOM in Visual Basic



Obtain a Reference to an XML
Document
Load or Create the Document
Navigate Through Its Nodes




Document
Elements
Attributes
Retrieve and Modify the XML Data
XML Overview
Getting Data from an XML Source


Load the Object with loadXML
XML String Parameter Allows Any URL
That Returns a Valid XML Document


Use any database publishing method in
SQL Server 2000
Once the Document Is Initialized, It Can
Be Navigated
Agenda






XML Overview
XML in SQL Server 2000
Database Publishing
OpenXML Rowsets
SQL Server 2000 XML Solutions
BizTalk Server 2000 Integration
XML in SQL Server 2000
Business Scenarios

Web Applications with Dynamic Data


Browser-based applications that require
data from a database
Business-to-Business Data Processing

Data interchange using XML as a



Ubiquitous
Extensible
Platform-independent data transport
mechanism
XML in SQL Server 2000
Areas of Support

Publishing a Database


T-SQL Language Extensions



Provides HTTP access through URLs to
templates and annotated schemas
FOR XML Clause in a SELECT Statement
Retrieves XML data from the database
engine
New OpenXML Syntax in T-SQL

Stores data into SQL Server
XML in SQL Server 2000
Publishing a Database

Many Forms of HTTP Access – Each
with a Different Purpose

URL queries are intended for debugging
and easy access in development or testing
environments


They are not intended for production sites
Templates and annotated schemas are
intended for production sites

They provide safe access by hiding T-SQL code
XML in SQL Server 2000
FOR XML Clause

SELECT Statements Now Have a New
Clause

FOR XML clause tells SQL Server 2000 the
results


Should be formatted as XML
FOR XML clause supports some modifiers:
AUTO, RAW, EXPLICIT

Example
SELECT *
FROM customers
FOR XML AUTO
XML in SQL Server 2000
OpenXML Syntax

OpenXML Clause Provides a Rowset
View of an XML Document


Can be used wherever a rowset provider
such as a table, view, or OpenRecordset
appears
T-SQL Provides Stored Procedures and
Clauses to Manipulate XML Data

With T-SQL, you can modify data
Agenda






XML Overview
XML in SQL Server 2000
Database Publishing
OpenXML Rowsets
SQL Server 2000 XML Solutions
BizTalk Server 2000 Integration
Database Publishing
Setting Up HTTP Access


HTTP Access Is Provided by an ISAPI
Filter that Maps the Request to SQL
Server 2000 Through OLE DB
An MMC Snap-In Provides the HTTP
Configuration Support with a Graphical
Interface

This tool creates the Microsoft Internet
Information Server virtual subdirectory
with the ISAPI filter
Database Publishing
Setting Up HTTP Access

Example

User sends HTTP request to retrieve SQL
Server 2000 data
3 ISAPI Filter
4 OLE DB
Customer
1
2
5
Internet
9
8
IIS
6
7
SQL
Then Data Goes
Back to Customer
Database Publishing
Setting Up HTTP Access

HTTP URL Format


http://domain/vroot/vname/…
SQL IIS Admin Features

Managing the virtual root





Security and settings
Virtual root physical path
Registering virtual names
Restarting HTTP access to SQL Server
2000
Managing multiple servers
Database Publishing
Setting Up HTTP Access Demo




Create HTTP Access for the Northwind
Database
Allow URL Queries to this Virtual Root
Browse the Customers Table
Shows What HTTP Access Is and How
To Set It Up on a Particular Server
Demo…
Database Publishing
FOR XML Clause



Besides the ISAPI Application and Its
Virtual Directory, the Engine Has to
Know Which Data Format to Use
SELECT Clause Now Supports the FOR
XML Clause
It Requests XML Results from SQL
Server 2000
SELECT *
FROM customers
FOR XML AUTO
Database Publishing
FOR XML Clause

FOR XML Clause Supports Three
Different Modifiers

Raw


Auto


Transforms each row in the result set into an
XML element with the generic identifier row
Returns query results in a simple, nested XML
tree
Explicit

Specifies the shape of the XML tree
Database Publishing
FOR XML Clause

Supports Two Optional Arguments

SchemaOption



Uses XMLData Schema specification
With this option, XMLData schema will be
returned
Elements


Columns are returned as sub-elements instead
of XML attributes
Auto mode only
Database Publishing
FOR XML Modes

EXPLICIT






Allows complete control over XML format
of XML result
Values in columns can be mapped to
attributes or sub-elements
Supports arbitrary nesting including
siblings and collapsing of hierarchy
Construction of ID/IDREF relationships
Supports CDATA sections in XML output
Nesting done based on PK/FK
relationships
Database Publishing
EXPLICIT Example
select 1 as TAG, NULL as PARENT,
‘P-’+ProductID as [Product!1!pid!id],
ProductName as
[Product!1!name!element],
NULL as [OrderDetail!2!oid]
from Products
union all
select 2, 1, ‘P-’+P.ProductID, NULL ,
OD.OrderID
from Products P inner join OrderDetails OD on
Products.ProductID=OrderDetails.ProductID
order by [Product!1!pid!id]
for xml explicit
Database Publishing
EXPLICIT Example (Results)
<Product pid ="P-2">
<Name>Chang</Name>
<OrderDetail oid="10258"/>
</Products>
…
Database Publishing
FOR XML Clause Demo


Code Walkthrough of SQL XML Viewer
Execute SQL XML Viewer


Use different FOR XML options
Shows How To



Use FOR XML clause
Use the DOM Inside Visual Basic
Manage SQL Server 2000 XML in Visual
Basic
Demo…
Database Publishing
Virtual Names

Establish the Initial Mapping to
Subdirectories Containing Annotated
Schemas and Templates

Virtual Names can be set by the SQL IIS
Admin, given a name, a path to directory or
file, and a type


Supports Dbobject, Schema, or Template types
Thus, Templates or Annotated Schemas
Can Be Referenced in the URL
http://localhost/Northwind/Customer/cust.xml
Database Publishing
Using Templates


Templates Are Equivalent to Method
Calls
Use Templates to Avoid Having the
T-SQL Code in the T-SQL Address
Query String


Then the browser shows only the resulting
code from the template
Like ASP Pages, the Actual Template
Requesting Code Is Not Shown
Database Publishing
Using Templates

Other Advantages of Using Templates




Can store several queries
Can contain an associated XSLT file, as
with any other XML file
Supports parameters and stored
procedures
Supports query schemas through XPaths
Database Publishing
Using Templates

Templates Are Accessed by Creating a
Virtual Name in SQL IIS Admin


The virtual name maps a directory where
files are stored
Templates Are Then Accessed by
References in the URL Using the Virtual
Name
http://localhost/Northwind/Templates/customer.xml
Database Publishing
Using Templates

Template Example
<?xml-stylesheet type="text/xsl"
href="Orders.xsl"?>
<ROOT xmlns:sql="urn:schemas-microsoftcom:xml-sql">
<sql:header>
<sql:param name="CustomerID">AAAA</sql:param>
</sql:header>
<sql:query>select OrderId, CustomerID,
OrderDate from Orders where
CustomerID=@CustomerID order by OrderDate for
XML auto, elements
</sql:query>
</ROOT>
Database Publishing
Using the Templates Demo




Walkthrough the customer.xml Template
File
Walkthrough the Customer.xsl
Transformation File
Create a Virtual Name Template
Browse the customer.xml File
Demo…
Database Publishing
Annotated Schemas

Provide Mapping Between XML and
Relational Schema




Uses annotations
Between elements and attributes in the
XMLdata schema
To tables and columns in a database
Relationships between XML hierarchy and
relational tables
Database Publishing
Annotated Schemas

sql:relation


sql:field


Establishes a mapping between an element
and a database table
Establishes a mapping between an element
and a database field
sql:relationship

Defines a relationship between two tables
or views in the database
Database Publishing
Annotated Schema Demo




Create a Virtual Name for Demo
Schemas
Walkthrough the Schema
Browse Schemas with Internet Explorer
Demonstrates How Different Elements
in the Schema Map to Different
Database Objects
Demo…
Database Publishing
XPaths



Query Rows in XML in the Virtual
Document Are Defined by Annotated
Schema
Thus, Elements Inside the Template or
Schema Can Be Referenced in the URL
Using the Nodes Hierarchy of an XML
Document
http://localhost/Northwind/Customer.xml/
OrderDetails
Database Publishing
XPaths

Example: E-Commerce Database

The XPath is Customer.xml/OrderDetails
Order Details
Customer
Vendor
Order
Product
Database Publishing
Using XPaths Demo



Shows How to Access Schemas With
XPaths
Demo Uses Template to Write the XPath
and the Pointer to the Schema File
Demo Selects Element Nodes and
Filters Data
Demo…
Agenda






XML Overview
XML in SQL Server 2000
Database Publishing
OpenXML Rowsets
SQL Server 2000 XML Solutions
BizTalk Server 2000 Integration
OpenXML Rowsets
XML Rowset Support
XML
DOM
XML
sp_xml_preparedocument
OpenXML
Table/View
FOR XML Clause
OpenXML Rowsets
OpenXML Clause

OpenXML Is Combined with T-SQL
Code


Use OpenXML as a relational view of XML
Execute any valid operation
INSERT INTO Customers(CustID, FirstName)
SELECT *
FROM
OpenXML(@idoc, 'Customer‘, 1)
WITH (CustID varchar(5),
FirstName nvarchar(30))
OpenXML Rowsets
OpenXML Clause



@idoc Parameter Is the XML Document
Reference
@rowpattern Is the XPath Pattern Used
to Select Rows of the View
@flags


Define if attributes or elements from
selected node are mapped to columns of
rows
With Clause

Defines exposed rowset and column
mapping for the view
OpenXML Rowsets
XML in T-SQL Demo

Walkthrough Demo Files



These files show how to use OpenXML to
create a Rowset from XML documents
Execute the Files Using Different
XPaths and Flags
Explains How To Manage XML
Documents Inside T-SQL Code
Demo…
Agenda






XML Overview
XML in SQL Server 2000
Database Publishing
OpenXML Rowsets
SQL Server 2000 XML Solutions
BizTalk Server 2000 Integration
SQL Server 2000 XML Solutions
Advantages


XML Enables Applications To Be Built
in a Loosely Coupled Manner
XML as Data Representation Has many
Advantages




XML was built to be inherently extensible
XML can be transmitted through firewalls
using standard internet protocols
XML can be transmitted across platforms
XML is a standard
SQL Server 2000 XML Solutions
Examples

Web Applications That Require Data
from Sources Outside of the Enterprise

Web Applications can expose SQL Server
2000 data to other applications in an easy
manner


Using a standard
Enterprise Applications That Need to
Adapt Easy to Data Changes
SQL Server 2000 XML Solutions
SQL XML and ADO XML Positioning


SQL Server 2000 Generates XML
Ranging from a Predefined Format to
Arbitrary Formats
ADO XML Generates Only a PreDefined Format

ADO 2.5 release supports generation of
hierarchical XML

Once again, in a pre-defined format
SQL Server 2000 XML Solutions
SQL XML and ADO XML Positioning

SQL Server 2000 Provides the
Flexibility Needed To Use Standard
XML Grammars


There Are Significant Performance Gains
by Using the SQL Server 2000
Implementation
ADO XML Should Be the Choice If a
Single Organization Controls All the
System

Programmers Can Use Existing Skills To
Access Data with ADO XML
Agenda






XML Overview
XML in SQL Server 2000
Database Publishing
OpenXML Rowsets
SQL Server 2000 XML Solutions
BizTalk Server 2000 Integration
Questions?
More Resources



http://msdn.microsoft.com/xml/
http://www.microsoft.com/sql/
http://www.w3.org/XML/