Template matching Template definition

Document Sample
Template matching Template definition Powered By Docstoc
					    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/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,
                                                                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
      <html>
         <head>
           <title>Whiskies of Scotland</title>
           <link rel="stylesheet" type="text/css" href="whisky_xsl.css"/>
         </head>
         <body>
           <h1>Whiskys of Scotland</h1>
                                                                      Apply matching
              <xsl:apply-templates/>
                                                                      templates in the
         </body>
                                                                       given element
      </html>
                                                                       sequence – all
   </xsl:template>
                                                                        children here
<!-- This matches (any) Distillery tag in the document.-->
                                                                Match the
                                                             element named
  <xsl:template match="Distillery">
                                                                Distillery
    <div class="main">
      <xsl:apply-templates/>
    </div>
  </xsl:template>

<!-- 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>
  </xsl:template>
        XSLT process

                  parameters

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
  dex.xql
                XML – a sample system
                         Browser


                       CSS process     Client


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



                          Java
      Example – Family History
                                    Person
         Photo
                      age
                               Name: string
Image: jpeg
Description: string
Date: date
Media: string
Inscription: string                  Event

                               Date : date




          Place
                       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>
                              <subject>
                                 <person>Robin Wallace</person>
Image: jpeg                   </subject>
Description: string           <subject>
Date: date                       <person>Kenneth Wallace</person>
                              </subject>
Media: string                 <subject>
Inscription: string              <age>12</age>
(Subject:                        <person>Francis Wallace</person>
    ( Person: name            </subject>
                              <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>
                                 <long>-3.06608</long>
                                 <lat>53.55555</lat>
                              </place>
                            </photo>
                 Example – Events
                         child
                                              Person
                     father
         Birth
                              mother     Name: string



<event>
   <class>Birth</class>                        Event
   <person>Francis Wallace</person>
   <date>1911-04-26</date>
   <father>                              Date : date
      <person>Kenneth Wallace</person>
   </father>
   <mother>
      <person>Ida Wallace</person>
   </mother>                              Death         Marriage
   <place>New Brighton</place>
 </event>
xquery version "1.0";
                                                                                 XQuery
(: 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)
                                    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>                                                             Evaluate an
          <link rel="stylesheet" href="photos.css" type="text/css"/>    expression and
          <title>Event List</title>
                                                                        insert the result
       </head>
       <body>
          <h2>
             <xsl:value-of select="$focus"/>
          </h2>
          <xsl:apply-templates/>
       </body>
    </html>
  </xsl:template>
                            XSLT …
<xsl:template match="eventList">
  <xsl:apply-templates/>                                         XPath Filter
  <hr/>                                                          conditions,
</xsl:template>
                                                               implicitly anded
<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>
              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                  Generalise
                                                (Trang)


XML document           XML Schema                     XML Document



           Validator            Form Builder
                                 (InfoPath)

                                                     Form Definition

analysis
                                Form engine
                                 (InfoPath)

                                                       XML Document

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:16
posted:7/4/2012
language:
pages:26