26
XSLT XML DBs , and Schemas Week 18 DSA

XSLT XML DBs , and Schemas

Embed Size (px)

DESCRIPTION

XSLT XML DBs , and Schemas. Week 18 DSA. The Whisky Case study. XSLT can be applied in the client. Add a xml processing instruction to the xml to bind to a XSLT script http://www.cems.uwe.ac.uk/~cjwallac/apps/scotch/ See listings of Distillery xml with link to stylesheet - PowerPoint PPT Presentation

Citation preview

XSLTXML DBs ,

and SchemasWeek 18

DSA

The Whisky Case study

• XSLT can be applied in the client.– Add a xml processing instruction to the xml to

bind to a XSLT script– http://www.cems.uwe.ac.uk/~cjwallac/apps/sc

otch/

– See listings of• Distillery xml with link to stylesheet• Stylesheet with link to CSS • CSS stylesheet

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- This stylesheet selects some elements from the whisky XML data, formats for display using classes. Only some tags are included - the remaining data is ignored. Connection with the css stylesheet is via class tags.

Note these classes should be documented --><!-- This Template matches the root of the document --> <xsl:template match="/"> <html> <head> <title>Whiskies of Scotland</title> <link rel="stylesheet" type="text/css" href="whisky_xsl.css"/> </head> <body> <h1>Whiskys of Scotland</h1> <xsl:apply-templates/> </body> </html> </xsl:template>

Matches the top-level Document

Elements written directly to the result

Apply matching templates in the given element sequence – all children here

<!-- This matches (any) Distillery tag in the document.-->

<xsl:template match="Distillery"> <div class="main"> <xsl:apply-templates/> </div> </xsl:template>

<!-- This matches (any) Name tag in the document.-->

<xsl:template match="Name"> <h2><xsl:value-of select='.'/></h2> </xsl:template>

<xsl:template match="Address"> <div class="em"><xsl:value-of select='.'/></div> </xsl:template>

Match the element named

Distillery

Evaluate an expression and insert the result Here just the text

in the current element

XSLT process

XML document

XSLT document

XSLT process XML or plain text

parameters

Parse inputs, set context to root;While nodes in context, for each node, check if any templates match – choose the most specific and apply the templateApply this recursively

An XML case study

• A combination of a photo album and a family history

• http://www.cems.uwe.ac.uk/chriswallace/index.xql

XML – a sample system

eXist Native XML Database

XQueryprocess

Java

Stores XML,XQuery,XSL

CSS and binary files

(JPEG)

XSLT process

CSS process

Server

Client

Browser

Example – Family History

Photo

Image: jpegDescription: stringDate: dateMedia: stringInscription: string

Person

Event

Name: string

Date : date

age

Place

Address : stringLat: decimalLong: decimal

Birth Death Marriage

Many-many resolution in XML

Photo

Image: jpegDescription: stringDate: dateMedia: stringInscription: string(Subject: ( Person: name Age: integer) | Animal : name)*Place: (address, lat, log) | name

<photo id="2"> <medium>B/W photograph</medium> <description>Family sitting round the fireplace</description> <subject> <person>Robin Wallace</person> </subject> <subject> <person>Kenneth Wallace</person> </subject> <subject> <age>12</age> <person>Francis Wallace</person> </subject> <subject> <animal>Cat</animal> </subject> <subject> <person>Miss Whitfield</person> </subject> <place> <address>Claremont, Brows Lane, Formby</address> <long>-3.06608</long> <lat>53.55555</lat> </place> </photo>

Example – EventsPerson

Event

Name: string

Date : date

Birth

Death Marriage

child

father

mother

<event> <class>Birth</class> <person>Francis Wallace</person> <date>1911-04-26</date> <father> <person>Kenneth Wallace</person> </father> <mother> <person>Ida Wallace</person> </mother> <place>New Brighton</place> </event>

xquery version "1.0";

(: List events for a person :)

declare namespace request="http://exist-db.org/xquery/request";declare namespace transform = "http://exist-db.org/xquery/transform";

let $person := request:request-parameter('person',''),(: get the set of events in which this person is involved :) $events := document(/'db/history/events.xml')/eventList/event[.//person = $person],(: get the stylesheet :) $ss := document('/db/history/eventList.xsl'),(: set the 'focus' of the event list to the person :) $params := <parameters> <param name="focus" value="{$person}"/></parameters>,(: order the events in ascending date order :) $elist := <eventList> {for $e in $events order by $e/date return $e }</eventList>

return(: return the event list transformed by the stylesheet :) transform:transform($elist,$ss,$params)

XQuery

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:param name="focus"/> <xsl:output method="html"/>

<xsl:template match="/"> <html> <head> <link rel="stylesheet" href="photos.css" type="text/css"/> <title>Event List</title> </head> <body> <h2> <xsl:value-of select="$focus"/> </h2> <xsl:apply-templates/> </body> </html> </xsl:template>

Evaluate an expression and insert the result

XSLT … <xsl:template match="eventList"> <xsl:apply-templates/> <hr/> </xsl:template>

<xsl:template match="event[class='Birth'][person = $focus]"> <h3> <xsl:value-of select="date"/> : Born at <xsl:value-of select="place"/> </h3> <p>Father <xsl:value-of select="father/person"/>, Mother <xsl:value-of select="mother/person"/> </p> </xsl:template> <xsl:template match="event[class='Birth'][father/person = $focus]"> <h3> <xsl:value-of select="date"/> : Child <xsl:value-of select="person"/> born at <xsl:value-of select="place"/> </h3> <p> Mother <xsl:value-of select="mother/person"/> </p> </xsl:template>

XPath Filter conditions,

implicitly anded

XSLT language

• Multilingual• Output • Imperative Control structures• Procedural programming• Template matching• Functional• XPath Functions• Process Control• Debugging

Multi-lingual

• Plain text and plain xml

• xsl (identified by the xsl:namespace)

• XPath to define node-sets• Filter conditions to select subsets• XPath functions to manipulate the nodes

• Comments– <xsl:comment>

Result output• Insert evaluated expression

• <xsl:value-of select=“exp”/>• Insert plain text

– <xsl:text>…. </xsl>• Insert processing instruction

– <xsl:processing-instruction>• Insert a number – like a level number

– <xsl:number>• Copy nodes into the output

– <xsl:copy-of>– <xsl:copy>

• Create a node– <xsl:element>– <xsl:attribute>

Imperative Control Structures

• Imperative Control structures– Sequence of data and instructions– Selection

• One option – <xsl:if >

• Multiple options– <xsl:choose>– <xsl:when>– <xsl:otherwise>

– Iteration• Iterate over nodes in a sequence

– <xsl:for-each>• Order nodes in the iteration

– <xsl:sort>

Procedural programming

• Procedure call - – <xsl:call-template name=“proc”>..

• Call Parameters – <xsl:with-param name=“x” select=“30”>

• Procedure definition – <xsl:template name=“proc”>..

• Procedure parameters – <xsl:param name=“x”>

Template matching

• Template definition– <xsl:templates match=“path”>

• Template application– <xsl:apply-templates select=“nodeset”>

• Recursive

• Apply most specific template to a node

• Declarative code a challenge to debug

Functional

• variables can be defined but they are constants– <xsl:variable name=“a” value=“exp”/>– <xsl:value-of select=“$a”/>

XPath Functions

• Boolean Functions

• String functions

• Numeric Functions

• Node sequence functions

Modular Programming

• Inclusion of another stylesheet– <xsl:include>

• Inclusion of tempates from a stylesheet– <xsl:import>

Process control

• Stylesheet declaration– <stylesheet>

• Output control– <xsl:output method=“text”/>

• Serialisation– <xsl:preserve-space>– <xsl:strip-space>

• Key– <xsl:key>

• Define attributes– <xsl:attribute-set>

Debugging

• Output Debug message– <xsl:message>

XML Schema

• A definition of the structure of a class of XML documents.

• Uses– Check that a given document is a member of

the class – ‘Validation’– http://tools.decisionsoft.com/schemaValidate/

– Determine the basic structure of a data entry form – InfoPath

XML Schema in action

QSEE

XML SchemaXML document

Validator

analysis

Form Builder(InfoPath)

Form Definition

Form engine(InfoPath)

XML Document

Generalise(Trang)

XML Document