xml ajax and ado.net
DESCRIPTION
XML AJAX and ADO.Net. XML: the big picture. Be wary… _______ changes EVERYTHING! Or _______ is a Paradigm Shift! For the last few years, technology pundits have XML in the blank. Microsoft is pushing XAML as part of the next version of Windows OS Extensible Application Markup Language - PowerPoint PPT PresentationTRANSCRIPT
1
XML AJAX and
ADO.Net
2
XML: the big picture
• Be wary…• _______ changes EVERYTHING!• Or _______ is a Paradigm Shift!• For the last few years, technology pundits
have XML in the blank.
• Microsoft is pushing XAML as part of the next version of Windows OS– Extensible Application Markup Language– http://msdn2.microsoft.com/en-us/library/ms752059.aspx
3
AJAX: the big picture
• Be wary…• _______ changes EVERYTHING!• Or _______ is a Paradigm Shift!• For the last few years, technology
pundits have AJAX in the blank.
• The main purpose of AJAX appears to be to bring web applications closer to the richness of interaction available to desktop applications.
4
AJAX: the big picture
• The primary “trick” which pulls this off is an asynchronous connection with the web server
• Remember, the web is a stateless environment (or at least the protocols are…)
• This separate communication line allows for “partial” updates and data transfer
5
AJAX: the big picture
• Partial updates require:– A means of identifying parts of a page or
more specifically a document: DOM– A standard way of transferring data to and
from a data source which is compatible with this model: XML
– A standard means of marking up these documents: XSL
– Let’s look at the XML portion first.
6
XML: the big picture
• Two world views: documents/data– Documents: semi-structured– Data: very structured (think ERD)
• The catalyst for convergence– The Internet and WWW
• XML: a data exchange format• Pundits have called this encounter a
“technology train wreck”
7
XML: the big picture
• The WWW defined a simple and universal standard for document exchange (HTML).– Information is decomposed into
named units, marked up, and transmitted•URL•HTML•HTTP
8
XML: the big picture
• The content of these named units of marked up information comes from somewhere…
• Databases!• Generating documents is most
often solved using a three-tier architecture.
9
XML: the big picture
• As an standard, HTML works well for publication, but not so well for data exchange.– Exchange requires screen
scraping and HTML parsing.– http://en.wikipedia.org/wiki/Screen_scraping
– Not a robust solution• XML provides a bridge between
systems (data exchange format)
10
Two cultures collide
• Web developers (Documents view)– Global infrastructure– Standards for document retrieval– Standards for materializing
documents– Semi-structured data
11
Two cultures collide
• Database view– Storage techniques– Standard query language– Efficient access to large
collections of highly structured data
– Data models for structuring data– Mechanisms for maintaining data
integrity and consistency
12
XML: the basics
• A caveat: XML is a VERY large (and often confusing) collection of emerging standards. – Our focus is on the convergence,
that is, the relation of XML to databases.
13
Semi-structured Data
• HTML describes how to present or render the content of an HTML document, that is the structure of the document.
• For example
14
Semi-structured Data
<HTML><BODY><TABLE BORDER=1><TR><TD>1</TD><TD>Davolio</TD><TD>Nancy</TD></TR><TR><TD>1</TD><TD>Fuller</TD><TD>Andrew</TD></TR><TR><TD>1</TD><TD>Leverling</TD><TD>Janet</TD></TR></TABLE></BODY></HTML>
15
Semi-structured Data
<HTML><BODY><TABLE BORDER=1><TR><TD>1</TD><TD>Davolio</TD><TD>Nancy</TD></TR><TR><TD>1</TD><TD>Fuller</TD><TD>Andrew</TD></TR><TR><TD>1</TD><TD>Leverling</TD><TD>Janet</TD></TR></TABLE></BODY></HTML>
Although both are human-readable (more-or-less), there is nothing to assist a software program in identifying the structure of the data.
16
Semi-structured Data
• HTML describes the structure of the document, but what is needed is the structure of the data.
• XML separates– Document structure (DTD)– Content (Elements, Attributes)– Materialization (XSL)
• In HTML, these are confounded
17
Semi-structured Data
• This confounding doesn’t matter for rendering and printing
• A problem arises when developers try to use these rendered documents to electronically exchange data– A natural extension of the analog
world really
18
XML • Moving from semi-structured data
to structured data and back again requires an abstraction of what a document contains– For example, online resume
system for Career Services– Abstract but specific to domain
• But abstraction isn’t enough– Documents must also follow rules
19
XML
• The idea of XML is that a document is defined by it’s structure, not by it’s formatting– Formatting is specific to
application or interface– Document structure defines the
data
20
XML: Basic Syntax• Elements
– A piece of text bounded by matching tags•<employee>Harold</employee>
•<element>content</element>– Elements (tags) can be anything– Elements can contain elements
as well as content
21
XML: Basic Syntax• Sub-elements
<EMPLOYEES><EMPLOYEEID>1</EMPLOYEEID><LASTNAME>Davolio</LASTNAME><FIRSTNAME>Nancy</FIRSTNAME>
</EMPLOYEES>
EmployeeID, LastName, and FirstName are sub-elements of Employee
22
XML: Basic Syntax• Sub-elements
– Describe the relation between an element and its component elements
– An Employee has an id, last name, and first name.
– The query analyzer in SQL Server will create this structure from a query
23
XML: Basic Syntax
Set nocount on;
SELECT EMPLOYEEID, LASTNAME, FIRSTNAME
FROM EMPLOYEES
FOR XML AUTO, ELEMENTS
I’ll discuss this syntax later.
24
XML: Basic Syntax
25
<HTML><BODY><TABLE BORDER=1><TR><TD>1</TD><TD>Davolio</TD><TD>Nancy</TD></TR><TR><TD>1</TD><TD>Fuller</TD><TD>Andrew</TD></TR><TR><TD>1</TD><TD>Leverling</TD><TD>Janet</TD></TR></TABLE></BODY></HTML>
Although the HTML and XML documents contain the same content, the HTML document describes how to present the data. The XML document describes the structure of the data.
26
XML: Basic Syntax• Attributes• XML allows the developer to
associate name/value pairs with an element.– Attributes can contain metadata about the
element or the content itself.– The current thinking seems to be that
elements should contain content and attributes should contain metadata.
– Page 457
27
SELECT EMPLOYEEID, LASTNAME, FIRSTNAMEFROM EMPLOYEESFOR XML RAW
Using the RAW option, the XML content can be represented as attributes.
28
In this case, I’ve include the data type of the element. Go to http://www.w3.org/2001/XMLSchema.xsd for the current standards.
29
XML: Basic Syntax• If the tags are balanced (match) and the
attributes are unique the XML document is said to be well-formed.– However, all this constraint ensures is
that the XML document will parse into a labeled tree.
– Is that sufficient for data exchange?– No.
• But first, let’s look at some more basic structure.
30
XML: Basic Syntax• The preceding example came from
just one table: Employees.• Most transactions will involve
multiple tables.• How are multiple tables
represented?– A tree– A graph
31
SELECT E.EMPLOYEEID, LASTNAME, ORDERID, CUSTOMERID, SHIPCITY, ORDERDATEFROM EMPLOYEES E, ORDERS OWHERE E.EMPLOYEEID = O.EMPLOYEEID AND ORDERDATE BETWEEN '1997-01-01' AND '1997-01-02'FOR XML AUTO, ELEMENTS
32
SELECT E.EMPLOYEEID, LASTNAME, ORDERID, C.CUSTOMERID, SHIPCITY, COMPANYNAMEFROM EMPLOYEES E, ORDERS O, CUSTOMERS CWHERE E.EMPLOYEEID = O.EMPLOYEEID AND C.CUSTOMERID=O.CUSTOMERID AND ORDERDATE BETWEEN '1997-01-01' AND '1997-01-02'FOR XML AUTO, ELEMENTS
33
XML
• Another example
SET NOCOUNT ON;SELECT CUSTOMERS.CUSTOMERID, CUSTOMERS.COMPANYNAME, ORDERS.ORDERID, ORDERS.ORDERDATE, [ORDER DETAILS].PRODUCTID, [ORDER DETAILS].QUANTITYFROM CUSTOMERS, ORDERS, [ORDER DETAILS]WHERE CUSTOMERS.CUSTOMERID=ORDERS.CUSTOMERID AND ORDERS.ORDERID=[ORDER DETAILS].ORDERID AND ORDERS.ORDERID=10258FOR XML AUTO, ELEMENTS
34
XML • So the output…
• Gets represented as an XML document as:
CUSTOMERID COMPANYNAME ORDERID ORDERDATE PRODUCTID QUANTITY ---------- ----------- -------- ---------- --------- -------- ERNSH Ernst Handel 10258 1996-07-17 2 50ERNSH Ernst Handel 10258 1996-07-17 5 65ERNSH Ernst Handel 10258 1996-07-17 32 6
35
XML
36
XML
37
XML: Basic Syntax• How to specify relationships?• Without a DTD, we can use
attributes to identify PKs and FKs.• ID and IDREF
38
<Northwind><E> <EMPLOYEEID ID="1"> <LASTNAME>Davolio</LASTNAME> </EMPLOYEEID>
<O><ORDERID>10400</ORDERID><CUSTOMERID>EASTC</CUSTOMERID><SHIPCITY>London</SHIPCITY><ORDERDATE>1997-01-01T00:00:00</ORDERDATE><EMPLOYEEID IDREF="1"/></O><O><ORDERID>10401</ORDERID><CUSTOMERID>RATTC</CUSTOMERID><SHIPCITY>Albuquerque</SHIPCITY><ORDERDATE>1997-01-01T00:00:00</ORDERDATE><EMPLOYEEID IDREF="1"/></O>
</E><E> <EMPLOYEEID ID="8"> <LASTNAME>Callahan</LASTNAME> </EMPLOYEEID>
<O><ORDERID>10402</ORDERID><CUSTOMERID>ERNSH</CUSTOMERID><SHIPCITY>Graz</SHIPCITY><ORDERDATE>1997-01-02T00:00:00</ORDERDATE><EMPLOYEEID IDREF="8"/></O>
</E></Northwind>
Primary Key
Foreign Key
Empty Element
39
40
XML: Basic Syntax• ID and IDREF can be used to
represent graphs– ERDs
• Minimum and maximum cardinalities can be defined– <EMPLOYEEID IDREF="1“ maxOccurs = “unbounded”/>
41
XML • But again, is it sufficient for an XML
document to well-formed?• No, we have to validate the data
conforms to the structure.• In databases we have mechanisms
for maintaining integrity and consistency.
• In XML, the mechanisms are– Document Type Definition (DTD)– XML Schema
42
DTD• The DTD serves as a grammar for
the underlying XML document– identifies the root document tag– declares what tags are permitted– the structure of the tags– the relations among tags
43
<!DOCTYPE Northwind [<!ELEMENT Northwind (EMPLOLYEES*)><!ELEMENT EMPLOYEES (EMPLOYEEID, LASTNAME, FIRSTNAME)><!ELEMENT EMPLOYEEID (#PCDATA)><!ELEMENT LASTNAME (#PCDATA)><!ELEMENT FIRSTNAME (#PCDATA)>
]><Northwind>
<EMPLOYEES><EMPLOYEEID>1</EMPLOYEEID><LASTNAME>Davolio</LASTNAME><FIRSTNAME>Nancy</FIRSTNAME>
</EMPLOYEES><EMPLOYEES>
<EMPLOYEEID>2</EMPLOYEEID><LASTNAME>Fuller</LASTNAME><FIRSTNAME>Andrew</FIRSTNAME>
</EMPLOYEES><EMPLOYEES>
<EMPLOYEEID>3</EMPLOYEEID><LASTNAME>Leverling</LASTNAME><FIRSTNAME>Janet</FIRSTNAME>
</EMPLOYEES></Northwind>
The first line denotes the root node of the XML document.The second line specifies <Northwind> can have an arbitrary number of <EMPLOYEES>.The third line specifies the sub-elements of <EMPLOYEES>The remaining lines define each sub-element. #PCDATA stands for Parsed Character Data.
All XML content is character string.
44
DTD• The order of elements in the <!
Element > tag are meaningful.• To be validated, the tags in the
XML document must conform to the order specified in the DTD.
45
DTD <!DOCTYPE Orderlist [ <!ELEMENT Orderlist (Customers)> <!ELEMENT Customers (CustomerID, CompanyName, Orders)>
<!ELEMENT CustomerID (#PCDATA)> <!ELEMENT CompanyName (#PCDATA)><!ELEMENT Orders (OrderID, OrderDate,ORDER_x0020_DETAILS)>
<!ELEMENT OrderID (#PCDATA)><!ELEMENT OrderDate (#PCDATA)>
<!ELEMENT Orders_x0020_DETAILS (ProductID, Quantity)><!ELEMENT ProductID (#PCDATA)><!ELEMENT Quantity (#PCDATA)>
]><ORDERLIST><CUSTOMERS>stuff…</CUSTOMERS></ORDERLIST>
46
DTD• We specify relationships with ID
and IDREF but in the <!ATTLIST > tag.
• The <!ATTLIST > tag allows us to assert specifics about the type of the attribute.
47
<!DOCTYPE Northwind [<!ELEMENT Northwind (E*)><!ELEMENT EMPLOYEES (EMPLOYEEID, LASTNAME, O)> <!ATTLIST E EMPLOYEEID ID #REQUIRED><!ELEMENT EMPLOYEEID (#PCDATA)><!ELEMENT LASTNAME (#PCDATA)><!ELEMENT O (ORDERID, CUSTOMERID, SHIPCITY, ORDERDATE, EMPLOYEEID)><!ELEMENT ORDERID (#PCDATA)><!ELEMENT SHIPCITY (#PCDATA)><!ELEMENT ORDERDATE (#PCDATA)><!ELEMENT EMPLOYEEID (#PCDATA)> <!ATTLIST E EMPLOYEEID IDREF #REQUIRED>
]><Northwind><E><EMPLOYEEID>1</EMPLOYEEID><LASTNAME>Davolio</LASTNAME>
<O><ORDERID>10400</ORDERID><CUSTOMERID>EASTC</CUSTOMERID><SHIPCITY>London</SHIPCITY><ORDERDATE>1997-01-01T00:00:00</ORDERDATE><EMPLOYEEID>1</EMPLOYEEID></O><O><ORDERID>10401</ORDERID><CUSTOMERID>RATTC</CUSTOMERID><SHIPCITY>Albuquerque</SHIPCITY><ORDERDATE>1997-01-01T00:00:00</ORDERDATE><EMPLOYEEID>1</EMPLOYEEID></O>
</E><E><EMPLOYEEID>8</EMPLOYEEID><LASTNAME>Callahan</LASTNAME>
<O><ORDERID>10402</ORDERID><CUSTOMERID>ERNSH</CUSTOMERID><SHIPCITY>Graz</SHIPCITY><ORDERDATE>1997-01-02T00:00:00</ORDERDATE<EMPLOYEEID>8</EMPLOYEEID></O>
</E></Northwind>
48
49
XSL• XML separates
– Document structure (DTD)– Content (Elements, Attributes)– Materialization (XSL)
• XSL transforms XML into HTML• The data model for XSL is an
ordered tree
50
XSL• XSL is defined as a set of template
rules• Each rule consists of a pattern and
a template• Transformation syntax permits
conversion of labeled tree into HTML
• For example
51
<HTML><BODY><H2>Employee List using HTML</H2><TABLE BORDER="1"><TR><TD>EMPLOYEEID</TD><TD>LASTNAME</TD><TD>FIRSTNAME</TD></TR><TR><TD>1</TD><TD>Davolio</TD><TD>Nancy</TD></TR><TR><TD>2</TD><TD>Fuller</TD><TD>Andrew</TD></TR><TR><TD>3</TD><TD>Leverling</TD><TD>Janet</TD></TR></TABLE></BODY></HTML>
Content can easily be generated from Northwind database dynamically. But again, the problem is how to get this back into the database or a different database.
52
<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="employeesDTD.xsl"?>
<!DOCTYPE Northwind [<!ELEMENT Northwind (EMPLOLYEES*)><!ELEMENT EMPLOYEES (EMPLOYEEID, LASTNAME, FIRSTNAME)><!ELEMENT EMPLOYEEID (#PCDATA)><!ELEMENT LASTNAME (#PCDATA)><!ELEMENT FIRSTNAME (#PCDATA)>
]><Northwind>
<EMPLOYEES><EMPLOYEEID>1</EMPLOYEEID><LASTNAME>Davolio</LASTNAME><FIRSTNAME>Nancy</FIRSTNAME>
</EMPLOYEES><EMPLOYEES>
<EMPLOYEEID>2</EMPLOYEEID><LASTNAME>Fuller</LASTNAME><FIRSTNAME>Andrew</FIRSTNAME>
</EMPLOYEES><EMPLOYEES>
<EMPLOYEEID>3</EMPLOYEEID><LASTNAME>Leverling</LASTNAME><FIRSTNAME>Janet</FIRSTNAME>
</EMPLOYEES></Northwind>
I’ve included a reference to an XSL stylesheet.
53
The stylesheet transforms the XML tree into a table.
54
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> <HTML> <HEAD><TITLE>Employee List using XSL Transformation</TITLE></HEAD> <BODY> <H2>Employee List using XSL Transformation</H2> <TABLE BORDER="1"> <TR><TD>EMPLOYEEID</TD><TD>LASTNAME</TD><TD>FIRSTNAME</TD></TR> <xsl:for-each select="Northwind/EMPLOYEES"> <TR> <TD><xsl:value-of select="EMPLOYEEID"/></TD> <TD><xsl:value-of select="LASTNAME"/></TD> <TD><xsl:value-of select="FIRSTNAME"/></TD> </TR> </xsl:for-each> </TABLE> </BODY> </HTML></xsl:template></xsl:stylesheet>
55
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> <HTML> <HEAD><TITLE>Employee List using XSL Transformation</TITLE></HEAD> <BODY> <H2>Employee List using XSL Transformation</H2> <TABLE BORDER="1"> <TR><TD>EMPLOYEEID</TD><TD>LASTNAME</TD><TD>FIRSTNAME</TD></TR> <xsl:for-each select="Northwind/EMPLOYEES"> <TR> <TD><xsl:value-of select="EMPLOYEEID"/></TD> <TD><xsl:value-of select="LASTNAME"/></TD> <TD><xsl:value-of select="FIRSTNAME"/></TD> </TR> </xsl:for-each> </TABLE> </BODY> </HTML></xsl:template></xsl:stylesheet>
Define the type of document with an xsl: begin and end tag with a reference to the w3 reference schema.
56
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> <HTML> <HEAD><TITLE>Employee List using XSL Transformation</TITLE></HEAD> <BODY> <H2>Employee List using XSL Transformation</H2> <TABLE BORDER="1"> <TR><TD>EMPLOYEEID</TD><TD>LASTNAME</TD><TD>FIRSTNAME</TD></TR> <xsl:for-each select="Northwind/EMPLOYEES"> <TR> <TD><xsl:value-of select="EMPLOYEEID"/></TD> <TD><xsl:value-of select="LASTNAME"/></TD> <TD><xsl:value-of select="FIRSTNAME"/></TD> </TR> </xsl:for-each> </TABLE> </BODY> </HTML></xsl:template></xsl:stylesheet>
Apply template to entire document starting with the root node.
57
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> <HTML> <HEAD><TITLE>Employee List using XSL Transformation</TITLE></HEAD> <BODY> <H2>Employee List using XSL Transformation</H2> <TABLE BORDER="1"> <TR><TD>EMPLOYEEID</TD><TD>LASTNAME</TD><TD>FIRSTNAME</TD></TR> <xsl:for-each select="Northwind/EMPLOYEES"> <TR> <TD><xsl:value-of select="EMPLOYEEID"/></TD> <TD><xsl:value-of select="LASTNAME"/></TD> <TD><xsl:value-of select="FIRSTNAME"/></TD> </TR> </xsl:for-each> </TABLE> </BODY> </HTML></xsl:template></xsl:stylesheet>
Hard code the basic structure of the table. This could be done in steps. The heading values could be retrieved using the xsl:ELEMENT.
58
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> <HTML> <HEAD><TITLE>Employee List using XSL Transformation</TITLE></HEAD> <BODY> <H2>Employee List using XSL Transformation</H2> <TABLE BORDER="1"> <TR><TD>EMPLOYEEID</TD><TD>LASTNAME</TD><TD>FIRSTNAME</TD></TR> <xsl:for-each select="Northwind/EMPLOYEES"> <TR> <TD><xsl:value-of select="EMPLOYEEID"/></TD> <TD><xsl:value-of select="LASTNAME"/></TD> <TD><xsl:value-of select="FIRSTNAME"/></TD> </TR> </xsl:for-each> </TABLE> </BODY> </HTML></xsl:template></xsl:stylesheet>
Scroll through each element in the tree starting with the “Northwind/EMPLOYEES” sub-elements. For each sub-element, create a row and column in the table.
59
XSL• I embedded the DTD in the XML
document, but if I had stored it as a separate file, the Employee List materialization would involve three distinct files
• Employees.xml (content)• Employees.dtd (structure)• Employees.xsl (presentation)
60
XML: data exchange • We can create an XML document
for the Product table for all products in category 4 that can be imported into the database.
SET NOCOUNT ONSELECT PRODUCTID, PRODUCTNAME, UNITSINSTOCK FROM PRODUCTS WHERE CATEGORYID=4FOR XML AUTO, ELEMENTS
61
XML • We can create an XML document
for the Product table for all products in category 4.
PRODUCTID PRODUCTNAME UNITSINSTOCK ----------- ---------------------------------------- ------------ 11 Queso Cabrales 2212 Queso Manchego La Pastora 8631 Gorgonzola Telino 032 Mascarpone Fabioli 933 Geitost 11259 Raclette Courdavault 7960 Camembert Pierrot 1969 Gudbrandsdalsost 2671 Flotemysost 2672 Mozzarella di Giovanni 14
62
XML
63
XML • This XML can then be parsed and
used by the DBMS. In this case, to insert the records into the local database.
SET NOCOUNT ONDECLARE @hdoc intDECLARE @doc varchar(4000)SET @doc ='<P><PRODUCTS><PRODUCTID>11</PRODUCTID>stuff…</ROOT>'EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc
SELECT * INTO ProductListCategory4 FROM OPENXML (@hdoc, '/P/PRODUCTS',2) WITH (PRODUCTID int, PRODUCTNAME varchar(25), UNITSINSTOCK int)EXEC sp_xml_removedocument @hdoc
64
XML • Sp_xml_preparedocument is a
system stored procedure that creates an internal tree representation of the XML document that can then be represented as a cursor or table using OPENXML.
• OPENXML creates a cursor or rowset representation that can then me used in a SQL expression.
• Sp_xml_removedocument clears the rows et from memory.
65
XML • Returning to the original claim that
XML will change everything• How is XML an improvement over
say SQL Server’s DTS?• Saving as comma delimited flat
file?
66
XML • XML is a language for manipulating
the what rather the how of data.• Standard means for representing
domains• Standard means of expressing
views• Clean separation of structure,
content, and formatting• Facility for document validity
checking
67
Extensible Style Language Transformation –XMLT
• XMLT is used to transform one document into another document
• Mapping company A’s document (say an order) into company B’s DTD or schema
68
XML Schema
• XML Schema is the preferred method for defining document structure
• The schema itself is an XML document• A document that conforms to an XML
Schema is termed schema-valid.• XML Schema documents are validated
against the root schema at www.w3.org
69
Does XML provide a reasonable solution?
• Web developers (Documents view)– Global
infrastructure– Standards for
document retrieval
– Standards for materializing documents
– Semi-structured data
• Database view– Storage techniques– Standard query
language– Efficient access to large
collections of highly structured data
– Data models for structuring data
– Mechanisms for maintaining data integrity and consistency