XSLT: Overview. An XSL transformation example. XPath in Short. 5. Document Transformations. Overview of XSLT Transformation

Similar documents
XML and Data Management

Markup Sprachen und semi-strukturierte Daten

XSLT - A Beginner's Glossary

Markup Languages and Semistructured Data - SS 02

XSL - Introduction and guided tour

XPath Processing in a Nutshell

T XML in 2 lessons! %! " #$& $ "#& ) ' */,: -.,0+(. ". "'- (. 1

Interactive Data Visualization for the Web Scott Murray

By Nabil ADOUI, member of the 4D Technical Support team

Data Integration through XML/XSLT. Presenter: Xin Gu

XML WEB TECHNOLOGIES

Introduction to XML Applications

XSLT Mapping in SAP PI 7.1

Terms and Definitions for CMS Administrators, Architects, and Developers

Database & Information Systems Group Prof. Marc H. Scholl. XML & Databases. Tutorial. 11. SQL Compilation, XPath Symmetries

10CS73:Web Programming

Course Information Course Number: IWT 1229 Course Name: Web Development and Design Foundation

Indexing XML Data in RDBMS using ORDPATH

IMPLEMENTING AN XML COURSE IN THE COLLEGE OF BUSINESS

Last Week. XML (extensible Markup Language) HTML Deficiencies. XML Advantages. Syntax of XML DHTML. Applets. Modifying DOM Event bubbling

Creating a TEI-Based Website with the exist XML Database

Extending the Linked Data API with RDFa

Web Development. Owen Sacco. ICS2205/ICS2230 Web Intelligence

DTD Tutorial. About the tutorial. Tutorial

Schematron Validation and Guidance

WWW. World Wide Web Aka The Internet. dr. C. P. J. Koymans. Informatics Institute Universiteit van Amsterdam. November 30, 2007

A LANGUAGE INDEPENDENT WEB DATA EXTRACTION USING VISION BASED PAGE SEGMENTATION ALGORITHM

WebSphere Business Monitor

An XML Based Data Exchange Model for Power System Studies

Firewall Builder Architecture Overview

Agents and Web Services

Web Services Technologies

Introduction to web development using XHTML and CSS. Lars Larsson. Today. Course introduction and information XHTML. CSS crash course.

Storing and Querying Ordered XML Using a Relational Database System

Interactive, dynamic Scalable Vector Graphics

Chapter 3: XML Namespaces

Presentation / Interface 1.3

Data XML and XQuery A language that can combine and transform data

Exchanger XML Editor - Canonicalization and XML Digital Signatures

Rigorous and Automatic Testing of Web Applications

CST6445: Web Services Development with Java and XML Lesson 1 Introduction To Web Services Skilltop Technology Limited. All rights reserved.

Extending XSLT with Java and C#

Overview of DatadiagramML

HTML, CSS, XML, and XSL

JISIS and Web Technologies

High Performance XML Data Retrieval

Managing large sound databases using Mpeg7

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 6 Professional Graduate Diploma in IT WEB ENGINEERING

Unified XML/relational storage March The IBM approach to unified XML/relational databases

Short notes on webpage programming languages

BASI DI DATI II 2 modulo Parte II: XML e namespaces. Prof. Riccardo Torlone Università Roma Tre

Java and XML parsing. EH2745 Lecture #8 Spring

Deferred node-copying scheme for XQuery processors

Enabling Web Services

Quiz! Database Indexes. Index. Quiz! Disc and main memory. Quiz! How costly is this operation (naive solution)?

Get Success in Passing Your Certification Exam at first attempt!

Structured vs. unstructured data. Motivation for self describing data. Enter semistructured data. Databases are highly structured

Change Management for XML, in XML

XML Processing and Web Services. Chapter 17

Introduction to Web Technologies

OOML: Structured Approach to Web Development

XML. CIS-3152, Spring 2013 Peter C. Chapin

Novell Identity Manager

Fast track to HTML & CSS 101 (Web Design)

Using SQL Queries in Crystal Reports

How To Use Query Console

Cleo Communications. CUEScript Training

aconferenceonxml Lesser Town Campus Prague, Czech Republic June 17-18, 2006

A User Interface for XML Document Retrieval

XML: extensible Markup Language. Anabel Fraga

Building A Very Simple Website

IBM Tivoli Federated Identity Manager V6.2.2 Implementation. Version: Demo. Page <<1/10>>

WTP-101 Developing Rich Internet Applications with Java Web Frameworks

Going Above and Beyond

Managing XML Documents Versions and Upgrades with XSLT

Facebook Twitter YouTube Google Plus Website

Pushing XML Main Memory Databases to their Limits

XML-Based Software Development

AnswerPath Version 5 User Guide Copyright 2014 Hindin Solutions Ltd. User Guide

Experiences with JSON and XML Transformations IBM Submission to W3C Workshop on Data and Services Integration October , Bedford, MA, USA

Lesson 4 Web Service Interface Definition (Part I)

IHE Radiology Technical Framework Supplement. Trial Implementation

We automatically generate the HTML for this as seen below. Provide the above components for the teaser.txt file.

Web Server Logs Analyze Using the XML Technologies

FileMaker Server 9. Custom Web Publishing with PHP

Structured vs. unstructured data. Semistructured data, XML, DTDs. Motivation for self-describing data

INFOPATH FORMS FOR OUTLOOK, SHAREPOINT, OR THE WEB

by LindaMay Patterson PartnerWorld for Developers, AS/400 January 2000

Basic Website Maintenance Tutorial*

Table and field properties Tables and fields also have properties that you can set to control their characteristics or behavior.

6. SQL/XML. 6.1 Introduction. 6.1 Introduction. 6.1 Introduction. 6.1 Introduction. XML Databases 6. SQL/XML. Creating XML documents from a database

REST vs. SOAP: Making the Right Architectural Decision

Semistructured data and XML. Institutt for Informatikk INF Ahmet Soylu

FileMaker Server 12. Custom Web Publishing with XML

XML Programming with PHP and Ajax

OData Extension for XML Data A Directional White Paper

Business Insight Report Authoring Getting Started Guide

XML and Data Integration

A Dataflow Graph Based Approach to Web Application Development

Collaborative editing using an XML protocol

Transcription:

5. Document Transformations XSLT: Overview XSLT (WC Rec. November 999) A language for transforming XML documents» representative of tree -based transformation languages (DSSSL, MetaMorphosis, TranSID, ) designed to be used» primarily as part of XSL formatting» also as an independent transformation language Our goal: to understand the basic model and central features of the language Overview and an example Data model and processing model XSLT uses XML syntax for expressing transformations of a document source tree into a result tree» result and source are separate trees by template rules Each (ordinary) template rules has a pattern (matched against nodes of the source tree) a template as a body» instantiated to create fragments of the result tree SDPL 00 Notes 5: XSLT SDPL 00 Notes 5: XSLT Overview of XSLT Transformation Style Sheets and Template Rules Source Document Source Tree Style Sheet Transformation Process Result Tree Output Process XML HTML Text An xsl:stylesheet (orxsl:transform) consists of template rules: <xsl:template match="pattern"> Template <!-- NB: well-formed XML --> Rule applied to nodes of source tree matched by the Pattern expressed using XML Path Language (XPath) Template consists of» literal result tree fragments (elements, text), and» XSLT instructions for creating further result tree fragments SDPL 00 Notes 5: XSLT SDPL 00 Notes 5: XSLT 4 XPath in Short An XSL transformation example WC Recommendation (November 999) a compact non-xml syntax for addressing parts of XML documents used also in other WC languages» XLink and XPointer (drafts for hyperlinks in XML)» XQuery (draft, /00, for querying XML) provides also typical primitives for manipulating strings, numbers and truth values Transform below document to HTML: <?xml version='.0'?> <?xml-stylesheet type="text/xsl" href="walsh.xsl"?> <!-- Modified from an example by Norman Walsh --> <doc><title>my Document</title> <para>this is a <em>short</em> document.</para> <para>it only exists to <em>demonstrate a <em>simple</em> XML document</em>.</para> <figure><title>my Figure</title> <graphic fileref="myfig.jpg"/></figure> </doc> SDPL 00 Notes 5: XSLT 5 SDPL 00 Notes 5: XSLT 6

Example style sheet begins Example (paras and emphs) <xsl:stylesheet version=".0" xmlns:xsl="http://www.w.org/999/xsl/transform"> <xsl:template match="/"> <!-- rule for root --> <HTML><HEAD><TITLE>A Document</TITLE></HEAD> <BODY> <!-- process root's children here: --> <xsl:apply-templates /> </BODY> </HTML> <xsl:template match="doc/title"> <H><xsl:apply-templates /></H> SDPL 00 Notes 5: XSLT 7 <xsl:template match="para"> <P><xsl:apply-templates /></P> <xsl:template match="em"> <I><xsl:apply-templates /></I> <xsl:template match="em/em"> <B><xsl:apply-templates /></B> SDPL 00 Notes 5: XSLT 8 Example (figures) Example (end of style sheet) <xsl:template match="figure"> <!-- Insert a bold caption of form 'Figure Num. ' by counting all figures in the document: --> <DIV><B>Figure <xsl:number level="any" count="figure"/>. </B> <BR /> <!-- Process the children of figure, --> <!-- the 'graphic' child first: --> <xsl:apply-templates select="graphic" /> <!-- then the 'title' child: --> <xsl:apply-templates select="title" /> </DIV> SDPL 00 Notes 5: XSLT 9 <xsl:template match="graphic"> <IMG> <!-- Create new attribute 'src': --> <xsl:attribute name="src"> <!-- and assign the value of current element's fileref attribute to it: --> <xsl:value-of select="@fileref" /> </xsl:attribute> </IMG> <!-- enclose figure title in a B element: --> <xsl:template match="figure/title"> <B> <xsl:apply-templates /> </B> <!-- for 'graphic' --> </xsl:stylesheet> SDPL 00 Notes 5: XSLT 0 Result (edited for readability) What use of XSL or XSLT? <!DOCTYPE html PUBLIC "-//WC//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD><TITLE>A Document</TITLE></HEAD> <BODY> <H>My Document</H> <P>This is a <I>short</I> document.</p> <P>It only exists to <I>demonstrate a <B>simple</B> XML document</i>.</p> <DIV> <B>Figure. </B> <BR> <IMG src="myfig.jpg"><b>my Figure</B> </DIV> </BODY> </HTML> SDPL 00 Notes 5: XSLT XSL can be used in different ways for offline document formatting» produce, say, PDF from XML by an XSL style sheet (using XSLT + XSL formatting objects) for offline document manipulation» transform XML into other form (XML/HTML/text) using XSLT for online document delivery» in a Web server» in a Web browser (if the browser supports) SDPL 00 Notes 5: XSLT

XSLT in online document delivery Data Model of XSLT and XPath XSLT in a browser defines rendering of XML documents approach of Microsoft Internet Explorer 5» transformation of XML to HTML on the fly in browser» NB: Microsoft's XSLT implementation differs from XSLT.0 (a new one exists but has to be installed separately) XSLT in a Web server an HTTP request for an XML document served by transforming XML on the fly to HTML (or other format) in the server Documents viewed as abstract, logical tree structures Seven types of tree nodes root (additional parent of document element) element nodes attribute nodes text nodes nodes for comments, processing instructionsand namespaces NB: Entities are expanded > no entity nodes SDPL 00 Notes 5: XSLT SDPL 00 Notes 5: XSLT 4 XSLT/XPath document trees XSLT/XPath trees: Example Element nodes have elements, text nodes, comments and processing instructions of their (direct) content as child nodes NB: attribute nodes not considered children the valueof an element node is the concatenation of its text-node descendants Nodes have a complete document order root node first, otherwise according to the order of the first character of the XML markup for each node > element node precedes it's attribute nodes, which precede any content nodes of the element SDPL 00 Notes 5: XSLT 5 Similar to the DOM structure model, with slight differences value of an element: its full textual content (In DOM: null) no names for text nodes, comment nodes, etc. (In DOM: "#text", "#comment", etc. Example document: <article>written by <fig file="pekka.jpg" caption="the Lecturer"/> the lecturer.</article> SDPL 00 Notes 5: XSLT 6 XSLT/XPath trees: Example XPath Expressions <article>written by <fig file="pekka.jpg" caption="the Lecturer"/> the lecturer.</article> text "" "Written by " root "" attribute "Written by the lecturer." element "article" "caption" "Written by the lecturer." element "fig" "" "The Lecturer" attribute SDPL 00 Notes 5: XSLT 7 "file" "pekka.jpg" text "" Legend: " the lecturer." type name value Used for selecting source tree nodes, conditional processing, and generating new text content return node-sets, truth values, numbers or strings can select any parts of source tree (node-set) for processing, using Location paths the most characteristic of XPath expressions evaluated with respect to a context node» often the current node matched by the template pattern result: set of nodes selected by the location path SDPL 00 Notes 5: XSLT 8

Location paths () Location paths () Consist of location steps separated by '/' each step produces a set of nodes steps evaluated left-to-right, each node in turn acting as a context node Complete form of a location step: AxisName:: NodeTest ([PredicateExpr])* axis specifies the tree relationship between the context node and the selected nodes node test restricts the type and and name of nodes further filtered by 0 or more predicates SDPL 00 Notes 5: XSLT 9 In total axes child, descendant, parent, ancestor, followingsibling, preceding-sibling, following, preceding, attribute, self, descendant-or-self, ancestor-orself, namespace Node tests (slightly simplified) Name: any element node with name Name (on an attribute axis, any attribute node with name Name ) *: any element node (on an attribute axis, any attribute node) text(): any text node node(): any node of any type SDPL 00 Notes 5: XSLT 0 Location paths () Abbreviations in location steps 'child::' can be omitted 'attribute::' can be shortened to '@' '/descendant-or-self::node()/' shortened to '//' ("go down any number of levels") 'self::node()' can be shortened to '.' (period) 'parent::node()' can be shortened to '..' Predicate '[position()=n]' for testing occurrence position n can be shortened to' [n]' '/' in the beginning sets context to the root node > Syntax resembles Linux/Unix file path names SDPL 00 Notes 5: XSLT Axes are oriented away from the context node (except attribute and namespace axes, which are unordered sets) the position() for the closest node = for the most remote node, position() = last() The simplest axis, self: Context node: SDPL 00 Notes 5: XSLT parent: ancestor: Context node: ancestor-or-self: SDPL 00 Notes 5: XSLT SDPL 00 Notes 5: XSLT 4 4

child: descendant: 4 Context node: 4 5 6 9 descendant-or-self: 7 8 5 8 9 4 6 7 0 SDPL 00 Notes 5: XSLT 5 SDPL 00 Notes 5: XSLT 6 preceding-sibling: following : following-sibling: preceding: SDPL 00 Notes 5: XSLT 7 SDPL 00 Notes 5: XSLT 8 Semantics of Location Paths (example) Location path examples () A context node A 5 7 B C B final value: {} 4 6 C 8 B "txt" */node()/parent::b[child::a] {, 5, 7} {, 4, 6, 8} {, 5, 7} chap children of current node:./chap (or equivalently: chap, or./child::node()[name()='chap']) The document element (child element of root node): /* Elements chapter anywhere (below the root): //chapter All chapters with attributetype="intro": //chapter[@type='intro'] the previous chapter sibling: preceding-sibling::chapter[] SDPL 00 Notes 5: XSLT 9 SDPL 00 Notes 5: XSLT 0 5

Location path examples () Location path examples () All child elements having an attribute type: *[@type] NB: use of node sets as truth values: empty - false; non -empty - true All child elements of any author child: author/* sections whose type attribute equals style attribute of the document element: //sect[@type = /*/@style] First author child, and previous to the last: author[], author[last()-] SDPL 00 Notes 5: XSLT Predicate expressions can be Boolean combinations: child elements author with apubl child, but without degree or award children: author[publ and not(degree or award)] Closest chap ancestor (and highest): ancestor::chap[] (ancestor::chap[last()]) author of the highest sect ancestor that is contained in anappendix: ancestor::sect[ancestor::app][last()]/author SDPL 00 Notes 5: XSLT XSLT Patterns XSLT Patterns: Semantics Main use in match attributes of template rules: <xsl:template match="pattern"> Restricted location path expressions: may use only child and attribute axes and operators'//' and '/'» can use arbitrary predicate expressions [Expr] may begin withid('idval') (for selecting element nodes by ID attribute values) alternative patterns separated by ' ' (node-set union operator) SDPL 00 Notes 5: XSLT A location path pattern P is of form Step Step Step n- Step n, where each separator is either '/' or '//' Pattern P matches a node n iff the rightmost step Step n matches n, and a suitable nodev matches the prefix Step Step Step n- of P : if the rightmost is '/', vmust be the parent of n if the rightmost is '//', v may be any ancestor of n That is, there's a path (Step n,..., Step ) from ntowards root SDPL 00 Notes 5: XSLT 4 XSLT Patterns: Examples XSLT Processing Model Pattern sect-head section/head matches any element with name sect-head, and any head elements directly below a section For a match with pattern /appendix//ulist/item[] a node has to be the first item element in aulist element which is contained in an appendix, which is the document element 0. Parse the document into a source tree. Construct the result tree by applying template rules to the source tree» may contain text, formatting objects and arbitrary elements. Serialize the result tree (as XML, HTML or text), or interpret it to produce formatted output (in case of full XSL processing) SDPL 00 Notes 5: XSLT 5 SDPL 00 Notes 5: XSLT 6 6

XSL Tree Construction Selecting among multiple matching rules Start at the source tree root; while(unprocessed source nodes left) do Find matching template rules; Choose one rule (the one with highest priority); Instantiaterule template in the context of the current node; Add the result to the result tree; Recursively process any source nodes selected by XSLT instructions (apply-templates, for-each) in the template; endwhile; Priority of a rule can be specified explicitly: <xsl:template priority=".0"> Default priorities based on the match pattern: 0 for simple name tests (likepara, @href) negative for less specific patterns e.g., *, @*, node() 0.5 for more complex patterns Multiple matching rules with the same maximum priority is an error - Processor may recover by (quietly!) choosing the last one of those rules SDPL 00 Notes 5: XSLT 7 SDPL 00 Notes 5: XSLT 8 Application of template rules Default rules for elements and content Without a select attribute <xsl:apply-templates /> processes all children of current node > Default behaviour: top-down traversal of source tree Otherwise the selected nodes are processed in document order (if not sorted with xsl:sort) Built-in rules allow recursive traversal to proceed gracefully in the absence of matching rules SDPL 00 Notes 5: XSLT 9 Built-in rule for the root and element nodes: <xsl:template match="/ *"> <xsl:apply-templates /> Built-in rule for text and attribute nodes: <xsl:template match="text() @*"> <!-- Insert the string value of current node: --> <xsl:value-of select="." /> Low priority > can be overridden SDPL 00 Notes 5: XSLT 40 A (Tricky) Processing Example Processing example () Consider transforming document <A> <C>c</C><B>bb<C>c</C></B><D>dd</D><C>c</C> </A> with the below rules: <xsl:template match="/"> <!-- Rule --> <R><xsl:apply-templates select="//b" /></R> <xsl:template match="b"> <!-- Rule --> <NewB>New: <xsl:apply-templates select="../c" /> <xsl:apply-templates /> </NewB> SDPL 00 Notes 5: XSLT 4 The result <R><NewB>New: ccbbc</newb></r> is obtained as follows:. Rule matches the root node > Element node R is added to the result; Instruction <xsl:apply-templates select="//b" /> selects the (only)b element for processing (which will produce the contents of node R).. Rule with pattern"b" creates into result tree anewb element node with text node "New: "as its first child. SDPL 00 Notes 5: XSLT 4 7

Processing example () Is it Really So Tricky?. Instruction <xsl:apply-templates select="../c"/> selects element Csiblings of current node (B). The built-in element rule applies to these, and the built-in text rule to their children. Result: text nodes "c" and "c" become the next children of NewB. 4. Instruction <xsl:apply-templates /> in the context of element node B selects its children, "bb" and <C>c</C>, for processing. The built-in text rule inserts value "bb" to the result tree, and thec element node becomes "c" in the result (similarly to step ). SDPL 00 Notes 5: XSLT 4 In practice: Seldom A computer scientist wants to understand how a model really works XSLT is high-level declarative language for describing transformations Normally no need to think about execution in so much detail; Often sufficient just to specify declarative rules to handle different cases, like <xsl:template match="para"> <P><xsl:apply-templates /></P> SDPL 00 Notes 5: XSLT 44 8