SQL Server 2000 XML Annotated Schemata Michael Rys Program Manager

advertisement
SQL Server 2000 XML
Annotated Schemata
Michael Rys
Program Manager
SQLServer XML Technologies
Microsoft Corporation
mrys@microsoft.com
Copyright 2000, Microsoft Corp.
Scenarios

Web applications with Dynamic Data


Browser based applications that require
data from a database
Business-to-business data
processing; messaging

Data interchange using XML as
ubiquitous, extensible, platform
independent transport format
Copyright 2000, Microsoft Corp.
Loosely Coupled Systems

Scalable. Many to Many.


Changes in Implementation do not break
each other
Move data in a standardized format:
 XML Syntax
 XML Schemata for domain standard
App
Mapping
Logic
Application System
Copyright 2000, Microsoft Corp.
Mapping Data
Object
(XML)
Data System
Challenges




Providing XML views
to relational tables
Allow efficient querying of XML
views
Allow updating of XML views
Provide other useful applications of
XML views:


Bulkload
Relational schema generation
Copyright 2000, Microsoft Corp.
View Example
Orders
ID
Cust
222
243
Order Lines
Order ID
222
Line No
1
Item
dt334
Quantity
1
Copyright 2000, Microsoft Corp.
<order ordid="222"
cust="243">
<line qty="1"
item="dt334"/>
</order>
XML Views




Defines an XML View on the database
Uses XML-Data Reduced (XDR) syntax
with annotations (mapping schemas)

Similar to DTD but using XML grammar

Public schema grammar used by BizTalk
Annotations specify the XML to
relational database mapping

For column values

For relationships between contained tags
XML View Mapper
Copyright 2000, Microsoft Corp.
Default Mapping
<?xml version="1.0"?>
<Schema name="Orders"
xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes">
<ElementType name="line" model="closed">
<AttributeType name="qty" />
<AttributeType name="item" />
<AttributeType name="line" />
<attribute type="qty" />
<attribute type="item" />
</ElementType>
<AttributeType name="ordid" />
<AttributeType name="cust" />
<ElementType name=order model="closed">
<attribute type="ordid" />
<attribute type="cust" />
<element type="line"/>
</ElementType>
</Schema>
Copyright 2000,
Microsoft Corp.
Default Mapping



Element name matches table name
Attribute and textOnly subelement
names match column names
No way to map hierarchies: resulting
XML is flat
Copyright 2000, Microsoft Corp.
Annotations
sql:relation (map table-element name)
sql:field (map column-element/attribute name)
sql:relationship (hierarchies, joins)
sql:limit-field
& sql:limit-value (horizontal partitions)
sql:key-fields (keys)
sql:id-prefix (XML prefix for IDs)
sql:is-constant (exists in XML, do not map to DB)
sql:map-field (exists in schema, do not map at all)
sql:overflow (column for unmapped XML)
sql:use-cdata (generate CDATA section)
sql:url-encode (contains dbobject reference)
sql:datatype (SQL datatype hint)
xmlns:sql=“urn:schemas-microsoft-com:xml-sql"
Copyright 2000, Microsoft Corp.
Annotations
<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<ElementType name="Customer" sql:relation="Customers">
<AttributeType name="ID" />
<attribute type="ID" sql:field="CustomerID" />
<element type="Order">
<sql:relationship key-relation="Customers"
key="CustomerID"
foreign-relation="Orders"
foreign-key="CustomerID"/>
</element>
</ElementType>
<ElementType name="Order" sql:relation="Orders">
<AttributeType name="OrderID" />
<attribute type="OrderID" sql:field="OrderID"/>
</ElementType>
</Schema>
Copyright 2000, Microsoft Corp.
XPath to define XML Views


W3C Recommendation
Allows to navigate XML trees:



SQLServer 2000 XPath:




Specifies set of nodes
Allows specification of predicates
Subset of W3C XPath 1.0 Recommendation
Defines virtual XML view over relational database
together with Annotated Schemas
Usable in URLs, Templates, and in ADO/OLE-DB
NOTE: OpenXML uses MSXML XPath
implementation
Copyright 2000, Microsoft Corp.
Syntax

General Syntax



/a::n[p]/b::m[q]
where a::n[p] location step; a, b axes; m, n
nodetests; p, q predicates
Shortform for most common axes
Example:


/child::Customer[attribute::State="WA"]
Shortform: /Customer[@State="WA"]
Copyright 2000, Microsoft Corp.
Semantics

General Semantics:


Select all nodes with name n for which
predicate p holds and can be reached via
axis a
p is existentially quantified
root
Customer
@state=WA
Customer
Customer
Order @state=CA Order Order
Order
@state=WA
/root /Customer [@state="WA"] /Order
Copyright 2000, Microsoft Corp.
SQLServer 2000 XPath Support








Axes: child, attribute, parent, self
Boolean-valued predicates
Relational operators =, !=, <=, >, >=
Arithmetic operators +, -, *, div
Boolean operators/functions AND, OR,
true(), false(), not()
Explicit conversion functions number(),
string(), boolean()
Variables
Not supported: See Books-OnLine
Copyright 2000, Microsoft Corp.
XPath and Annotated Schema
SQL DB
Annotated
Schema
Defines set
of potential
XML views
over DB
Copyright 2000, Microsoft Corp.
XPath Query
/A/B/C
First location step A
defines instance of
XML view
over DB
Querying

XPath Query



Used with a reference to the
mapping schema
/Customer[@ID=“ALFKI”]
Results in:
<Customer ID="ALFKI">
<Order OrderID="10643"
<Order OrderID="10692"
<Order OrderID="10702"
<Order OrderID="10835"
<Order OrderID="10952"
<Order OrderID="11011"
</Customer>
Copyright 2000, Microsoft Corp.
/>
/>
/>
/>
/>
/>
Functional XPath Limitations


Not a full Query Language, only navigation
XPath cannot project



Tailor annotated schema to project only accessible
data
Use XSLT postprocessing to project data needed
in query but not exposed in view
XPath cannot prune subtrees (e.g., give me all
customers of state X and only their orders from today)



Static pruning: map to SQL views
Dynamic pruning: XSLT
XPath cannot transform


Tailor annotated schema
Use XSLT to transform
Copyright 2000, Microsoft Corp.
URL Query


http://server/vroot/vname/xpath?params
Virtual name (vname)


Direct reference to mapping schema
Path to directory


XPath Query (xpath)



http://server/vroot/vname/nwind.xml/xpath/?params
Specifies set of nodes from virtual document
to return
Encode + etc. into %xx in URL
Parameters (params)


xsl, outputencoding, contenttype, root, …
User defined
Copyright 2000, Microsoft Corp.
Template


http://server/vroot/vname?params
Same as template query with
embedded SQL Query except:



Embedded query references mapping
schema file
Query is expressed as XPath
Supports querying over namespace
qualified names
Copyright 2000, Microsoft Corp.
Template (Example)
<root xmlns:sql="urn:schemas-microsoftcom:xml-sql"
sql:xsl="path to XSLT file" >
<sql:header>
<sql:param name="state">WA</sql:param>
</sql:header>
<sql:xpath-query mappingschema="nwind.xdr">
/Customers[@Region=$state]
</sql:xpath-query>
</root>
Copyright 2000, Microsoft Corp.
Template Processing
URL
XML
CustState
.xml
nwind
.xdr
Computes
Request from
ISAPICustState.xml
Using nwind.xdr
Middle-Tier
Copyright 2000, Microsoft Corp.
TDS/
XML
SQL
FOR XML
Computed
FOR XML
EXPLICIT
Data
SQL Server 2000
BizTalk Integration

BizTalk
Schema
Uses same schema format as BizTalk
framework and server
copy
SQL
Server
BizTalk
Schema
+
=
Mapping
Schema
+
Annotations
XPATH
/Customers
Copyright 2000, Microsoft Corp.
XML
in
BizTalk
Grammar
UpdateGrams







Expresses Modifications to an
XML document
Uses optimistic concurrency control
Captures inserts, deletes, and updates
Supports hierarchical updates
to multiple tables using
annotated schemas
Allows specification of null image
Values can be parameterized (uses
$param, see DB202 for syntax)
Currently ships in beta as web release
Copyright 2000, Microsoft Corp.
UpdateGrams
<r xmlns:updg="urn:schemas-microsoft-com:xml-updategram">
<updg:sync mapping-schema="nwind.xml"
updg:nullvalue="ISNULL">
<updg:before>
<Customer CustomerID="LAZYK" CompanyName="ISNULL"
Address="12 Orchestra Terrace" >
<Order oid="10482"/>
<Order oid="10545"/>
</Customer>
</updg:before>
<updg:after>
<Customer CustomerID="LAZYK"
CompanyName="Lazy K Country Store"
Address="12 Opera Court" >
<Order oid="10354"/>
<Order oid="10545"/>
</Customer >
</updg:after>
</updg:sync>
</r>
Copyright 2000, Microsoft Corp.
Bulkload







Allows loading of large amount of XML
into database via annotated schema.
Allows generation of relational schema
Provided as COM object
Can be called from DTS
Uses SAX to parse XML
Provides transacted and non-transacted
modes
Beta will ship Oct 2000 as web release
Copyright 2000, Microsoft Corp.
Bulkload - Rules


Only limited buffering while loading
Record Lifetime Rule


Basic Record Generation Rules


A new record is generated for a new relation on
node or if a sql:relationship exists for this node
Subset Lifetime Rule


Data for a record needs to be grouped in the same
element subtree
FK of 1:n relationships is buffered only as long as
parent that defines FK is in scope
Key Ordering Rule

FK of 1:n needs to appear before many children
Copyright 2000, Microsoft Corp.
Bulkload - Interface
ISQLXMLBulkLoad {
ConnectionString(…);
ConnectionCommand(…);
BulkLoad(…);
KeepNulls(…);
KeepIdentity(…);
CheckConstraints(…);
ForceTableLock(…);
Transaction(…);
XMLFragment(…);
SchemaGen(…);
SGDropTables(…);
SGUseID(…);
ErrorLogFile(…);
Execute(…); }
Copyright 2000, Microsoft Corp.
Summary



Use XML-centric application
programming model
Queryable and updatable XML view
over database
Provides:

Simple, declarative method for
defining XML views on the database



NO CODING REQUIRED
Mechanism to retrieve XML
conforming to Biztalk
schema definitions
XSL for arbitrary output
Copyright 2000, Microsoft Corp.
Future







Web Release Model
Updategrams (Aug-Nov 2000)
Bulk Load (Oct-Nov 2000)
XML View Mapper (Sep-Nov 2000)
W3C Schemas (XSD)
W3C Query Language
Performance
Copyright 2000, Microsoft Corp.
Download