+ All Categories
Home > Documents > XML, XSLT, Xquery, Web Servicesdb.ucsd.edu/static/cse136S08--archive/XML-XQuery-XSLT.pdf ·...

XML, XSLT, Xquery, Web Servicesdb.ucsd.edu/static/cse136S08--archive/XML-XQuery-XSLT.pdf ·...

Date post: 12-Jul-2020
Category:
Upload: others
View: 21 times
Download: 0 times
Share this document with a friend
29
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
Transcript
Page 1: 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

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

Page 2: 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

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" >&nbsp;<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

Page 3: 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

3

...What’s Wrong with HTML...

<DT><IMG SRC= "greenball.gif" >&nbsp;<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

XML is Based on Markup

<bibliography>

<paper ID= "object-fusion"> <authors>

<author>Y.Papakonstantinou</author><author>S. Abiteboul</author><author>H. Garcia-Molina</author>

</authors> <fullPaper source="fusion"/><title>Object Fusion in Mediator Systems</title> <booktitle>VLDB 96</booktitle>

</paper>

</bibliography>

Markup indicatesstructure and semantics

Decoupled from presentation

Page 4: 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

4

Elements and their Content

element

element name

Character content

ElementContent

EmptyElement

<bibliography>

<paper ID="object-fusion"> <authors>

<author>Y.Papakonstantinou</author><author>S. Abiteboul</author><author>H. Garcia-Molina</author>

</authors> <fullPaper source="fusion"/><title>Object Fusion in Mediator Systems</title> <booktitle>VLDB 96</booktitle>

</paper>

</bibliography>

Element Attributes

<bibliography>

<paper ID="object-fusion"> <authors>

<author>Y.Papakonstantinou</author><author>S. Abiteboul</author><author>H. Garcia-Molina</author>

</authors> <fullPaper source="fusion"/><title>Object Fusion in Mediator Systems</title> <booktitle>VLDB 96</booktitle>

</paper>

</bibliography>

Attribute name

Attribute Value

XML = Labeled Ordered Trees

<bibliography><paper id=23...>

<authors><author>Yannis</author><author>Serge</author>...

</authors> <title>Object Fusion</title>

...</paper>

</bibliography>

bibliography

paper

authors

author author

...

titlefullpaper

Yannis Serge

Object Fusion...

paper

≈ semistructured data ≈ labeled trees/graphs

can also represent• relational and • object-oriented data

@id

23

Page 5: 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

5

How do I sharestructure and

metadata/semanticswith my project leader,

web designer,developer?

Communicating Structure

Adding Structure and Semantics

• XML Document Type Definitions (DTDs):• define the structure of "allowed" documents (i.e.,

valid wrt. a DTD)

• ≈ database schema => improve query formulation, execution, ...

• XML Schema – defines structure and data types

• XML Namespaces– identify your vocabulary

• Resource Description Framework (RDF)– simple metadata model

XML DTDs as Extended CFGs

<!element bibliography paper*><!element paper (authors,fullPaper?,title,booktitle)><!element authors author+>

bibliography paper*paper authors fullPaper? title booktitleauthors author+

lhs = element (name)rhs = regular expression over elements + strings (PCDATA)

XML DTD

Grammar

Page 6: 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

6

<!element bibliography paper*><!element paper (authors, fullPaper?, title, booktitle)><!element authors author+><!element author (#PCDATA)><!element fullPaper EMPTY><!element title (#PCDATA)><!element booktitle (#PCDATA)><!attlist fullPaper source ENTITY #REQUIRED> <!attlist paper ID ID>

Document Type Definitions (DTDs)

Define and Constrain Element Names & Structure

Element TypeDeclaration

Attribute ListDeclaration

Element Declarations

<!element bibliography paper*><!element paper (authors, fullPaper?, title, booktitle)><!element authors author+><!element author (#PCDATA)>

<!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

Page 7: 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

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

Page 8: 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

8

Uses of XML Entities

• Physical partition – size, reuse, "modularity", … (both XML docs &

DTDs)

• Non-XML data– unparsed entities → binary data

• Non-standard characters– character entities

• Shorthand for phrases & markup

From Docs to Data: XML Schema

• 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>

</character></book>

Page 9: 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

9

The Simple “Russian Doll” Approach to XML Schema

<?xml version="1.0" encoding="utf-8"?><xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"><xsd:element name="book"><xsd:complexType><xsd:sequence><xsd:element name="title" type="xsd:string"/><xsd:element name="author" type="xsd:string"/><xsd:element name="character“

minOccurs="0" maxOccurs="unbounded"><xsd:complexType><xsd:sequence><xsd:element name="name" type="xsd:string"/><xsd:element name="friend-of" type="xsd:string“

minOccurs="0" maxOccurs="unbounded"/><xsd:element name="since" type="xsd:date"/><xsd:element name="qualification" type="xsd:string"/>

</xsd:sequence> …<xsd:attribute name="isbn" type="xsd:string"/>

Optional Namespace Definition

Sequence Compositor Simple TypeContent fortitle andauthor

Complex TypeContent for book

Character may appear any number of times

Basic Type of XML Schema

The Catalog Approach to XML Schema: Stand-Alone Declarations & References

<xsd:element name="title" type="xsd:string"/><xsd:element name="author" type="xsd:string"/><xsd:element name="name" type="xsd:string"/>…<xsd:attribute name="isbn" type="xsd:string"/>

<xsd:element name="character"><xsd:complexType><xsd:sequence><xsd:element ref="name"/><xsd:element ref="friend-of”

minOccurs="0" maxOccurs="unbounded"/><xsd:element ref="since"/><xsd:element ref="qualification"/>

</xsd:sequence></xsd:complexType>

</xsd:element>

Simple TypeElements

Attributes

Complex TypeElement character

Reference

Catalog Approach Cont’d

<xsd:element name="book"><xsd:complexType><xsd:sequence><xsd:element ref="title"/><xsd:element ref="author"/><xsd:element ref="character“

minOccurs="0" maxOccurs="unbounded"/></xsd:sequence><xsd:attribute ref="isbn"/>

</xsd:complexType></xsd:element>

Page 10: 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

10

Named Types

• Write stand-alone named complex type or simple type declarations

• Primitive form of inheritance (called derivation) allows– Restriction– Extension

<xsd:simpleType name="nameType"><xsd:restriction base="xsd:string"><xsd:maxLength value="32"/></xsd:restriction></xsd:simpleType>

<xsd:complexType name="characterType"><xsd:sequence><xsd:element name="name“ type="nameType"/><xsd:element name="friend-of“

type="nameType”minOccurs="0“ maxOccurs="unbounded"/>

<xsd:element name="since" type="sinceType"/>

<xsd:element name="qualification" type="descType"/>

</xsd:sequence> </xsd:complexType>

nameType derived from xsd:string by havingthe xsd:maxLength facet restrict string to a

Maximum of to 32 characters

nameType used in the declaration of characterType

Groups: Named containers of sets of Elements or Attributes

<xsd:group name="mainBookElements"><xsd:sequence><xsd:element name="title" type="nameType"/><xsd:element name="author" type="nameType"/>

</xsd:sequence></xsd:group>

<xsd:complexType name="bookType"><xsd:sequence><xsd:group ref="mainBookElements"/><xsd:element name="character" type="characterType“

minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence>

</xsd:complexType>

Compositors: Sequence, Choice, All

<xsd:group name="nameTypes"><xsd:choice><xsd:element name="name" type="xsd:string"/><xsd:sequence><xsd:element name="firstName" type="xsd:string"/><xsd:element name="middleName" type="xsd:string“

minOccurs="0"/><xsd:element name="lastName" type="xsd:string"/>

</xsd:sequence></xsd:choice>

</xsd:group>

So far we have seensequences

The group nameTypes consists of one of• the element “name”• the sequence containing firstName,

middlename, lastName

Page 11: 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

11

Compositors (cont’d)

<xsd:complexType name="characterType"><xsd:all><xsd:element name="name“ type="nameType"/><xsd:element name="friend-of“ type="nameType”

minOccurs="0“ maxOccurs="unbounded"/><xsd:element name="since" type="sinceType"/><xsd:element name="qualification" type="descType"/></xsd:all> </xsd:complexType>

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">

<xsd:union><xsd:simpleType><xsd:restriction base="xsd:string"><xsd:pattern value="[0-9]{10}"/></xsd:restriction>

</xsd:simpleType>

<xsd:simpleType><xsd:restriction base="xsd:NMTOKEN"><xsd:enumeration value="TBD"/><xsd:enumeration value="NA"/></xsd:restriction>

</xsd:simpleType></xsd:union>

</xsd:simpleType>

The simple type isbnType will be either• a 10-digit string (notice the pattern)• the token "TBD“ or the token "NA"

Constraints: Uniqueness

<xsd:element name="book">…<xsd:unique name="charNameMustBeUnique"><xsd:selector xpath="character"/><xsd:field xpath="name"/>

</xsd:unique>…

</xsd:element>

By inserting xsd:unique in the book element declarationwe enforce that the character name’s in each book are unique

Page 12: 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

12

Namespaces

<xsd:schema xmlns:xsd=http://www.w3.org/2000/10/XMLSchemaxmlns=http://example.org/ns/books/targetNamespace=http://example.org/ns/books/elementFormDefault="qualified“attributeFormDefault="unqualified" >

Including Unknown Elements

<xsd:complexType name="descType" mixed="true"><xsd:sequence><xsd:any namespace=http://www.w3.org/1999/xhtml

minOccurs="0" maxOccurs="unbounded“processContents="skip"/>

</xsd:sequence></xsd:complexType>

Turning XML into HTML: XSLT

• Why Stylesheets?

– separation of content (XML) from presentation (XSL)

• Why not just CSS for XML?

– XSL is far more powerful:

• selecting elements

• transforming the XML tree

• content based display (result may depend on data)

Page 13: 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

13

XSLT Overview

• XSLT stylesheets are denoted in XML syntax

• XSL components:

1. a language for transforming XML documents (XSLT: integral part of the XSL specification)

2. an XML formatting vocabulary(Formatting Objects: >90% of the formatting properties inherited from CSS)

XSLT Processing Model

XML source tree XML,HTML,… result tree

XSL stylesheet

Transformation

XSLT Processing Model

• XSL stylesheet: collection of template rules• template rule: (pattern ⇒ template)• main steps:

– match pattern against source tree– instantiate template (replace current node “.” by the

template in the result tree)– select further nodes for processing

• control can be– program-driven ("pull": <xsl:foreach> ...)– data/event-driven ("push": <xsl:apply-templates> ...)

Page 14: 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

14

<xsl:template match="product"> <table>

<xsl:apply-templates select="sales/domestic"/> </table> <table> <xsl:apply-templates select="sales/foreign"/>

</table> </xsl:template>

Template Rule: Example

(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

</xsl:element>

<xsl:template match="person"><p><xsl:value-of select="@first-name"/><xsl:text> </xsl:text><xsl:value-of select="@surname"/></p>

</xsl:template>

Page 15: 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

15

Example of Turning XML into HTML

<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="FitnessCenter.xsl"?><FitnessCenter>

<Member level="platinum"><Name>Jeff</Name><Phone type="home">555-1234</Phone><Phone type="work">555-4321</Phone><FavoriteColor>lightgrey</FavoriteColor>

</Member></FitnessCenter>

HTML Document in an XSL Template

<?xml version="1.0"?><xsl:output method="html"/><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Tran

version="1.0">

<xsl:template match="/"><HTML>

<HEAD><TITLE>Welcome</TITLE>

</HEAD><BODY>

Welcome!</BODY>

</HTML></xsl:template>

</xsl:stylesheet>

Extracting the Member Name

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

version="1.0">

<xsl:output method="html"/>

<xsl:template match="/"><HTML>

<HEAD><TITLE>Welcome</TITLE>

</HEAD><BODY>

Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>!</BODY>

</HTML></xsl:template>

</xsl:stylesheet>

(see html-example02)

Page 16: 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

16

Extracting a Value from an XML Document,

Navigating the XML Document

• Extracting values:– use the <xsl:value-of select="…"/> XSL

element• Navigating:

– The slash ("/") indicates parent/child relationship

– A slash at the beginning of the path indicates that it is an absolute path, starting from the top of the XML document

/FitnessCenter/Member/Name

"Start from the top of the XML document, go to the FitnessCenter element, from there go to the Member element, and from there go to the Name element."

Document/

PI<?xml version=“1.0”?>

ElementFitnessCenter

ElementMember

ElementName

ElementPhone

ElementPhone

ElementFavoriteColor

TextJeff

Text555-1234

Text555-4321

Textlightgrey

Extract the FavoriteColor and use it as the bgcolor

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

version="1.0">

<xsl:output method="html"/>

<xsl:template match="/"><HTML>

<HEAD><TITLE>Welcome</TITLE>

</HEAD><BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}">

Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>!</BODY>

</HTML></xsl:template>

</xsl:stylesheet>

(see html-example03)

Page 17: 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

17

Note

Attribute values cannot contain "<" nor ">"- Consequently, the following is NOT valid:

<Body bgcolor="<xsl:value-of select='/FitnessCenter/Member/FavoriteColor'/>">

To extract the value of an XML element and use it as an attributevalue you must use curly braces:

<Body bgcolor="{/FitnessCenter/Member/FavoriteColor}">

Evaluate the expression within the curly braces. Assign the valueto the attribute.

Extract the Home Phone Number<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

version="1.0">

<xsl:output method="html"/>

<xsl:template match="/"><HTML>

<HEAD><TITLE>Welcome</TITLE>

</HEAD><BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}">

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">

– Repetition...

Page 18: 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

18

Creating the Result Tree: Repetition

<xsl:template match="/"><html>

<head><title>customers</title>

</head><body>

<table><tbody><xsl:for-each select="customers/customer">

<tr><th><xsl:apply-templates select="name"/>

</th><xsl:for-each select="order"><td><xsl:apply-templates/>

</td>...

</html></xsl:template>

Creating the Result Tree: Sorting

<xsl:template match="employees"><ul><xsl:apply-templates select="employee"><xsl:sort select="name/last"/><xsl:sort select="name/first"/></xsl:apply-templates></ul>

</xsl:template>

<xsl:template match="employee"><li>

<xsl:value-of select="name/first"/><xsl:text> </xsl:text><xsl:value-of select="name/last"/>

</li></xsl:template>

More on XSL

• XSL(T):– Conflict resolution for multiple applicable rules – Modularization <xsl:include> <xsl:import>

– …

• XSL Formatting Objects– a la CSS

• XPath (navigation syntax + functions)= XSLT ∩ XPointer

• ...

Page 19: 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

19

XQuery: Querying XML Sources

• Functional Query Language– Operates on the Xpath/XQuery data model– List of ordered trees– A document is list of size 1

• XQuery expressions are composed of– Path expressions– Element constructors– FLWR expressions– … and more …

chapter

Path Expressions

doc(“zoo.xml”)//chapter[2]//figure[caption=“tree frogs”]

In the second chapter of the document zoo.xml find the figures with caption “Tree Frogs”

book

chapter chapter appendixpart

section

paragraph

figure

caption

“Tree Frogs”

chapter

chapter

paragraph

figure

caption

“Just Frogs”

part

More Path ExpressionsFind the first immediate chapter subelements of immediate part subelements of the document zoo.xml and retrieve

figures that have …doc(“zoo.xml”)/part/chapter[1]//figure[caption=“Tree Frogs”]

chapter

book

chapter chapter appendixpart

section

paragraph

figure

caption

“Tree Frogs”

chapter

chapter

paragraph

figure

caption

“Just Frogs”

part

Page 20: 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

20

Element Construction

<result>{ doc(“zoo.xml”)//chapter[2]//figure[caption=“Tree Frogs”]}

</result>

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>

Page 21: 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

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

Page 22: 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

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>

Page 23: 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

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}

{IF count($b/author) < 3$b/authorELSE$b/author[1], <author>and others</author>}

</short>

Page 24: 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

24

Existential and Universal Quantification

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

Page 25: 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

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

XMLFile

XQuery Processor XQuery

DOMObject

SAXStream

Typical Scenario: XML Messaging

WrapperRDBMS

WrapperSAP ERP

ApplicationRequests innative language

or specialwrapper API

SELECT *FROM Customer, OrderWHERE customer.name=“Joe”AND order.name=“Joe”

Cdom = Sap(conn1, “joe”)

SOAP serviceMessage

Transformer

Summary of Steps

Developer’sProgramIssues

SQL Query

Wrapper returns

SQL result wrapped as

XML message

Developer’sXQuery

transformsXML message to

XML formatneeded by

app

Page 26: 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

26

Typical Scenario: XML Messaging

WrapperRDBMS

WrapperSAP ERP

Application

SOAP serviMessage

Transformer

<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>

Page 27: 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

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

Page 28: 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

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

Page 29: 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

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>


Recommended