View
215
Download
1
Tags:
Embed Size (px)
Citation preview
DSA
Semester 2
XML
• Tagged data<h1>Hello</h1><module code=“UFIE8K” level=“3”>
<review id=“fred>A really interesting course, well taught</review>
</module>
• Interchange of data• RSS, BPEL4WS, RossettaNet …
• Structure document representation• Module specifications
• Languages• XSLT, XSchema, Soap
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 seet 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>
Matches the top-level Document
Apply matching templates in the given node-set
Evaluate an expression and insert the result
Elements written directly to 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>
Match the node named eventList
XPath Filter conditions,
implicitly anded
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
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>