Template matching Template definition

Document Sample
Template matching Template definition Powered By Docstoc
 XML DBs ,
and Schemas
  Week 18
      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

  – 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,
                                                                Matches the top-
  formats for display using classes.
                                                                level Document
   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 --> Elements written
   <xsl:template match="/">                              directly to the result
           <title>Whiskies of Scotland</title>
           <link rel="stylesheet" type="text/css" href="whisky_xsl.css"/>
           <h1>Whiskys of Scotland</h1>
                                                                      Apply matching
                                                                      templates in the
                                                                       given element
                                                                       sequence – all
                                                                        children here
<!-- This matches (any) Distillery tag in the document.-->
                                                                Match the
                                                             element named
  <xsl:template match="Distillery">
    <div class="main">

<!-- This matches (any) Name tag in the document.-->
                                                                 Evaluate an
  <xsl:template match="Name">                                 expression and
    <h2><xsl:value-of select='.'/></h2>                       insert the result
  </xsl:template>                                            Here just the text
                                                                in the current
  <xsl:template match="Address">                                   element
    <div class="em"><xsl:value-of select='.'/></div>
        XSLT process


XML document

                XSLT process           XML or plain text

XSLT document
                   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 template
                   Apply this recursively
        An XML case study
• A combination of a photo album and a
  family history
• http://www.cems.uwe.ac.uk/chriswallace/in
                XML – a sample system

                       CSS process     Client

                              XSLT      Server
Stores XML,
XQuery,XSL               XQuery
  CSS and                process
 binary files
   (JPEG)                                       Native XML
                          eXist                  Database

      Example – Family History
                               Name: string
Image: jpeg
Description: string
Date: date
Media: string
Inscription: string                  Event

                               Date : date

                       Birth    Death         Marriage
Address : string
Lat: decimal
Long: decimal
    Many-many resolution in XML
                           <photo id="2">
                              <medium>B/W photograph</medium>
          Photo               <description>Family sitting round the fireplace</description>
                                 <person>Robin Wallace</person>
Image: jpeg                   </subject>
Description: string           <subject>
Date: date                       <person>Kenneth Wallace</person>
Media: string                 <subject>
Inscription: string              <age>12</age>
(Subject:                        <person>Francis Wallace</person>
    ( Person: name            </subject>
      Age: integer) |            <animal>Cat</animal>
      Animal : name           </subject>
)*                            <subject>
                                 <person>Miss Whitfield</person>
Place:                        </subject>
   (address, lat, log) |      <place>
    name                         <address>Claremont, Brows Lane, Formby</address>
                 Example – Events
                              mother     Name: string

   <class>Birth</class>                        Event
   <person>Francis Wallace</person>
   <father>                              Date : date
      <person>Kenneth Wallace</person>
      <person>Ida Wallace</person>
   </mother>                              Death         Marriage
   <place>New Brighton</place>
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 =
(: get the stylesheet :)
     $ss := document('/db/history/eventList.xsl'),
(: set the 'focus' of the event list to the person :)
     $params :=
   <param name="focus" value="{$person}"/>
(: order the events in ascending date order :)
     $elist :=
   {for $e in $events
    order by $e/date
    return $e

(: return the event list transformed by the stylesheet :)
<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="/">
       <head>                                                             Evaluate an
          <link rel="stylesheet" href="photos.css" type="text/css"/>    expression and
          <title>Event List</title>
                                                                        insert the result
             <xsl:value-of select="$focus"/>
                            XSLT …
<xsl:template match="eventList">
  <xsl:apply-templates/>                                         XPath Filter
  <hr/>                                                          conditions,
                                                               implicitly anded
<xsl:template match="event[class='Birth'][person = $focus]">
     <xsl:value-of select="date"/> : Born at <xsl:value-of select="place"/>
  <p>Father <xsl:value-of select="father/person"/>,
       Mother <xsl:value-of select="mother/person"/>
<xsl:template match="event[class='Birth'][father/person = $focus]">
     <xsl:value-of select="date"/> :
       Child <xsl:value-of select="person"/>
       born at <xsl:value-of select="place"/>
  <p> Mother <xsl:value-of select="mother/person"/>
              XSLT language
•   Multilingual
•   Output
•   Imperative Control structures
•   Procedural programming
•   Template matching
•   Functional
•   XPath Functions
•   Process Control
•   Debugging
• 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
• variables can be defined but they are
  – <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>
• 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                  Generalise

XML document           XML Schema                     XML Document

           Validator            Form Builder

                                                     Form Definition

                                Form engine

                                                       XML Document

Shared By: