XML, XSLT, Xquery, Web Services and their role in Web Application programming Evolving as We Speak… XML-based Model 2 (MVC) Architecture of today Model - Controller View: XSLT-based presentation XML/ XML Schema-defined interaction XML-based Model 2 (MVC) Architecture of tomorrow Controller View: XSLT-based presentation XML/ XML Schema-defined interaction Model uses Web services and XML-wrapped sources XQuery or WSDL requests / XML results
29
Embed
XML, XSLT, Xquery, Web Servicesdb.ucsd.edu/static/cse136S08--archive/XML-XQuery-XSLT.pdf · 2005-11-17 · 1 XML, XSLT, Xquery, Web Services and their role in Web Application programming
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
XML, XSLT, Xquery, Web Services and their role in Web Application
programmingEvolving as We Speak…
XML-based Model 2 (MVC) Architecture of today
Model - Controller
View:XSLT-based presentation
XML/ XML Schema-defined interaction
XML-based Model 2 (MVC) Architecture of tomorrow
Controller
View:XSLT-based presentation
XML/ XML Schema-defined interaction
Model uses Web services andXML-wrapped sources
XQuery or WSDL requests / XML results
2
Overview of our XML Standards
• Motivation: HTML vs XML• XML 101: syntax, elements, attributes,
DTDs, …• XML 201: XML Schema, Namespaces • XSLT: Transforming and Rendering XML• XQuery: Search, Transform & Integrate
So what is XML (all about)?
Executive Summary:• XML = HTML – idiosyncrasies (simplified syntax)
+ user-definable ("semantic") tags• Separation of data and its presentation
=> simple, very flexible data exchange format:semistructured data model
=> new applications: • Information exchange (B2B), sharing (diglib), integration
("mediation"), archival, ...• Web application development:
• Cleaner separation of View from Controller• Unifying data model for multiple information sources
What’s Wrong with HTML?
<DT><IMG SRC="greenball.gif" > <A NAME="object-fusion"></A>Y.Papakonstantinou, S. Abiteboul, H. Garcia-Molina. <A HREF="http://www-cse.ucsd.edu/~yannis/papers/fusion.ps">"ObjectFusion in Mediator Systems".</A> In <I>VLDB 96.</I></DT>
Y. Papakonstantinou, S. Abiteboul, H. Garcia-Molina.“Object Fusion in Mediator Systems”. In VLDB 96.
HTML confuses presentationwith content
3
...What’s Wrong with HTML...
<DT><IMG SRC= "greenball.gif" > <A NAME="object-fusion"></A>Y.Papakonstantinou, S. Abiteboul, H. Garcia-Molina. <A HREF="http://www-cse.ucsd.edu/~yannis/papers/fusion.ps">"ObjectFusion in Mediator Systems".</A> In <I>VLDB 96.</I></DT>
No Explicit Structure,Semantics, or Object-Orientation
Author
ConferenceTitle
... And Some Repercussions
=> HTML is inappropriate for
− data exchange
− Separation of logical from presentation aspects of a Web application’s view
<!element fullPaper EMPTY><!element title (#PCDATA)><!element booktitle (#PCDATA)><!attlist fullPaper source ENTITY #REQUIRED><!attlist paper ID ID>
Character content
Authors followed byoptional fullpaper,followed by title,
followed by booktitle
Sequence of 1 ormore author
Sequence of 0 ormore paper
Element Content Declarations
Declaration Meaning element name Exactly one instance of element R? Zero or one instances of R R* Zero or more instances of R R+ One or more instances of R R1|R2|…|Rn One instance of R1 or R2 or … Rn #PCDATA Character content EMPTY Empty element (#PCDATA e*)* Mixed Content ANY Anything goes
7
Attributes
<bibliography>
<paper ID="object-fusion" ROLE="publication">
<authors><author authorRef="yannis">
Y.Papakonstantinou</author></authors>
<fullPaper source="fusion"/><title>Object Fusion in Mediator Systems</title><related papers= "semistructured-data" "mediators"/>
</paper>
</bibliography>
Object Identity Attribute
CDATA (character data)
<person ID="yannis"> Yannis’ info </person>
IDREFintradocument
reference
Reference toexternal ENTITY
Attribute Types
Type MeaningID Token unique within the documentIDREF Reference to an ID tokenIDREFS Reference to multiple ID tokensENTITY External entity (image, video, …)ENTITIES External entitiesCDATA Character dataNMTOKEN Enumerated tokenNMTOKENS Enumerated tokensMore toappear?
More types (eg, DATE) may soon bepart of the standard
More on Attribute Declarations
• Attributes may be– REQUIRED– IMPLIED (optional)– can have default values– default value may be FIXED
• XML DTDs (part of the XML spec.)– flexible, semistructured data model (nesting,
ANY, ?, *, |, ...) – but document-oriented (SGML heritage)
• XML Schema (W3C working draft)– schema definition language in XML– data-oriented: data types– extends capabilities of DTD
Sample Data for Introduction to XML Schema
<?xml version="1.0" encoding="utf-8"?><book isbn="0836217462"><title>Being a Dog Is a Full-Time Job</title><author>Charles M. Schulz</author><character><name>Snoopy</name><friend-of>Peppermint Patty</friend-of><since>1950-10-04</since><qualification> extroverted beagle </qualification>
</character><character><name>Peppermint Patty</name><since>1966-08-22</since><qualification>bold, brash and tomboyish</qualification>
The characterType consists of name, a list of friend-of,since, and qualification particles in no particular order.(Compare with the sequence compositor.)
Derivation of Simple Types:Unions and Lists So far we
have seenrestrictions and facets<xsd:simpleType name="isbnType">
(i) match pattern: process <product> elements(ii) instantiate template: replace each a product with two HTML tables(iii) select the <product> grandchildren (“sales/domestic”,
“sales/foreign”) for further processing
pattern
template
Match/Select Patterns
• match patterns ⊂ select patterns = defined in http://w3.org/TR/xpath
• Examples: – /mybook/chapter[2]/section/*
– chapter|appendix
– chapter//para
– div[@class="appendix" and position() mod 2 = 1]//para
– ../@lang
Creating the Result Tree...• Literal result elements: non-XSL elements (e.g.,
HTML) appear “literally” in the result tree• Constructing elements:
(similar for xsl:attribute, xsl:text, xsl:comment,…)
• Generating text:
<xsl:element name = "…">attribute & children definition
Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>!<BR/>Your home phone number is:<xsl:value-of select="/FitnessCenter/Member/Phone[@type='home']"/>
</BODY></HTML>
</xsl:template>
</xsl:stylesheet>
(see html-example04)
Creating the Result Tree...
• Further XSL elements for ...– Numbering
• <xsl:number value="position()" format="1 ">
– Conditions• <xsl:if test="position() mod 2 = 0">
In the second chapter of the document zoo.xml find the figures with caption “Tree Frogs” and place them into
an element called result
figure
caption
“Tree Frogs”
result
Bibliography Example Data Set<bib><book><author> Aho </author><author> Hopcroft </author><author> Ullman </author><title> Automata Theory </title><publisher> Morgan Kaufmann </publisher><year> 1998 >/year></book><book><author> Ullman </author><title> Database Systems </title><publisher> Morgan Kaufmann </publisher><year> 1998 >/year></book><book><author> Abiteboul </author><author> Buneman </author><author> Suciu </author><title> Automata Theory </title><publisher> Prentice Hall </publisher><year> 1998 >/year></book></bib>
Reviews Example Data Set<reviews><review><title> Automata Theory </title><comment> It’s the best in automata theory </comment><comment> A definitive textbook </comment></review>…</reviews>
21
For-Let-Where-Return (FLWR)
FOR $b in doc(“bib.xml”)//bookWHERE $b/publisher = “Morgan Kauffman”RETURN $b/title
List the titles of books published by “Morgan Kaufmann”
year
bib
book book book
publisher
MorganKaufmann
yearpublisher
MorganKaufmann
1998 1998
book
yearpublisher
PrenticeHall
1998
title title title
Think (tuples of) variable bindings
FOR/LET
WHERE
RETURN
Ordered lists of tuplesof variable bindings
Tuples of thatsatisfy the conditions
List of trees
$bbookbookbook
$bbookbook
title title
year
year
bib
book book
publisher
MorganKaufmann
yearpublisher
MorganKaufmann
1998 1998
book
yearpublisher
PrenticeHall
1998
title title title
FOR $b in doc(“bib.xml”)//bookWHERE $b/year > 1990RETURN $b/author
Return the list of authors who published after 1990
22
Tuples
FOR $p in distinct(doc(“bib.xml”)//publisher)LET $b := document(“bib.xml”)//book[publisher = $p]WHERE count($b) > 1RETURN $p
List publishers who have publishedmore than 1 book
Tuples ($p, $b) are formulated
Boolean Expressions in WHERE
FOR $b in doc(“bib.xml”)//bookWHERE $b/publisher = “Morgan Kaufmann”AND $b/year = “1998”
RETURN $b/title
List the titles of books published by “Morgan Kaufmann” in 1998
JoinsFOR $b in doc(“bib.xml”)/book,$r in doc(“review.xml”)/review
WHERE $b/title = $r/titleRETURN <book_with_review>{$b/@*}{$b/*}{$r/comment}
</book_with_review>
For every book with a matching review outputa book_with_review
that contains all the attributesand subelements of book
and the commentsubelements of review
<book_with_review><author> Aho </author><author> Hopcroft </author><author> Ullman </author><title> Automata Theory </title><publisher> Morgan Kaufmann </publisher><year> 1998 >/year><comment> It’s the best in automata theory </comment><comment> A definitive textbook </comment></book with review>
23
Relax Order Conditions
FOR $b in unordered(doc(“bib.xml”)//book)WHERE $b/publisher = “Morgan Kaufmann”AND $b/year = “1998”
RETURN $b/title
List the titles of books published by “Morgan Kaufmann” in 1998
Very important feature in dealing withrelational sources and other set-oriented sources.
SELECT titleFROM bibWHERE publisher = “Morgan Kaufmann” AND year =1998
Depending on the indices and access methods used, the SQL query processor may deliver the tuples in
different order
Nested queries
FOR $a IN distinct(document(“bib.xml”)//author)RETURN
<author><name> {$a/text()} </name>{FOR $b IN document(“bib.xml”)//book[author=$a]RETURN $b/title
}</author>
Invert the structure of the input document so that there is a list of author elements containing the
name of the author and the list of books he wrote
Conditionals
FOR $b IN doc(“bib.xml”)/bookRETURN<short>{$b/title}
FOR $b in doc(“bib.xml”)/bookWHERE $b/author = “Ullman”RETURN $b
FOR $b in doc(“bib.xml”)/bookWHERE EVERY $author IN $b/author
SATISFIES $author= “Ullman”RETURN $b
Return books where at least one of the authors is “Ullman”
Return books where all authors are “Ullman”
Functions
DEFINE FUNCTION depth($e) RETURNS xsd:integer{IF empty($e/*) THEN 1ELSE max(for $c in $e/* return depth($c)) + 1}
FOR $b in doc(“bib.xml”)/book RETURN depth($b)
Applicability of XML Query Languages (Xquery)
• XQuery standard does NOT elaborate on the physical aspects of the XML sources
• Custom functions can provide access and reference to the source(s) – document(“test.xml”), source(“view1”)
• Question: as we go down the list of uses of XQuery compare with XSL
25
XQuery on files, DOM objects, event streams, messages
• Usage scenarios– Transformation and processing of messages
• Significant (but not “killer”) advantages over XSL– Minor performance optimization superiority– Better streaming, pipelining– Cleaner extensible language
• Many academic and industrial prototypes of XQuery on files
<query_result><customer><name> Joe </name><balance>100M</balance><order> fish… </order></customer><customer><name> Joe </name><balance>100M</balance><order> meat… </order></customer></query result>
<customer><name> Joe </name><due> 780M </due><orders><order>fish</order><order>meat</order></orders></customer>
FOR $cn IN distinct(msg(123)/customer/name)RETURN <customer>$cn<due>7.8 * msg(123)/customer[name=$cn]/balance</due><orders>FOR $c IN msg(123)/customerWHERE $c/name = $cnRETURN {$c/order}
</orders></customer>
Direct XQuery on Databases
Xquery Processor
RDBMS
XML View of Relational DB
tuple
reldb
orders customers
name
tuple
balance
Joe 100M
XQuery
SQL (oneor more)
tuples
XMLresult
Let’s write a Russian
Doll schema
XQuery on Relational Databases FOR $c IN db(1)/customers/tupleWHERE $c/name = “Joe”RETURN <customer>$c/name<due>7.8 * $c/balance</due><orders>FOR $o IN db(1)/orders/tupleWHERE $c/name = $o/nameRETURN $o</orders></customer> Xquery Processor
RDBMS
XML View of Relational DB
SELECT * FROM customersWHERE name = “Joe”
For each customer #cSELECT * FROM ordersWHERE orders.name = #c.name
Merge results
<customer><name> Joe </name><due> 780M </due><orders><order>fish</order><order>meat</order></orders></customer>
27
Summary of Steps
Developer’sProgramIssues
Xquery onXML view of
SQL DB
XqueryProcessor automatically
sends SQL queriesto DB and structures
XML result
XQuery on Relational Databases
• Single language for accessing database and structuring XML result
• Avoids deficiencies of SQL in dealing with nested structures, optional elements, etc
• …
XQuery on Distributed Sources
Xquery Processor (Mediator)
RDBMS
XML View of All Sources
RDBMS
XQuery XMLresult
XMLFile
28
Example: Access to Two Relational Databases
Xquery Processor (Mediator)
RDBMS(orders)
XML View of All Relational DBs
RDBMS(customers)
XQuery XMLresult
FOR $c IN db(1)/customers/tupleWHERE $c/name = “Joe”RETURN <customer>$c/name<due>7.8 * $c/balance</due><orders>FOR $o IN db(2)/orders/tupleWHERE $c/name = $o/nameRETURN $o</orders></customer>
XQuery on Integrated Views
Xquery Processor (Mediator)
RDBMS
Virtual Integrated XML View
RDBMS
XQuery XMLresult
XMLFile
customer
view
customers
name
customer
balance
Joe 100M
orders
order order order
Let’s write the “Joe”
query again
and using XQuery to build the view
Xquery Processor (Mediator)
RDBMS
Virtual Integrated XML View
RDBMS
XQuery XMLresult
XMLFile
XQuery asView
Definition
29
View = Query
FOR $c IN db(1)/customers/tupleRETURN <customer>$c/name<due>7.8 * $c/balance</due><orders>FOR $o IN db(2)/orders/tupleWHERE $c/name = $o/nameRETURN $o</orders></customer>