SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based.

Slides:



Advertisements
Similar presentations
XML: Extensible Markup Language
Advertisements

Bottom-up Evaluation of XPath Queries Stephanie H. Li Zhiping Zou.
Internet Technologies1 1 Lecture 4: Programming with XSLT.
Managing Data Exchange: XPath
XPath XML Path Language. Outline XML Path Language (XPath) Data Model Description Node values XPath expressions Relative expressions Simple subset of.
Transforming XML Part I Document Navigation with XPath John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:
XPath Eugenia Fernandez IUPUI. XML Path Language (XPath) a data model for representing an XML document as an abstract node tree a mechanism for addressing.
SDPL 2002Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based.
Rendering XML documents with XSL The most powerful approaches to rendering XML documents involve using XSL (eXtensible Stylesheet Language) XSL enables.
XSL Transformations Lecture 8, 07/08/02. Templates The whole element is a template The match pattern determines where this template applies Result element(s)
XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
XPath Carissa Mills Jill Kerschbaum. What is XPath? n A language designed to be used by both XSL Transformations (XSLT) and XPointer. n Provides common.
Lecture 12. Default Processing in XSLT The default processing in XSLT is to process the XPath root node The default processing for various node types.
XPath Tao Wan March 04, What is XPath? n A language designed to be used by XSL Transformations (XSLT), Xlink, Xpointer and XML Query. n Primary.
Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – Carl Lagoze – Cornell University.
Object Oriented Programming III1 XSLT Processing XML using XSLT Using XPath.
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
Internet Technologies1 XSLT Processing XML using XSLT Using XPath.
Overview of XPath Author: Dan McCreary Date: October, 2008 Version: 0.2 with TEI Examples M D.
Introduction to XPath Bun Yue Professor, CS/CIS UHCL.
SD2520 Databases using XML and JQuery
10/06/041 XSLT: crash course or Programming Language Design Principle XSLT-intro.ppt 10, Jun, 2004.
Sheet 1XML Technology in E-Commerce 2001Lecture 6 XML Technology in E-Commerce Lecture 6 XPointer, XSLT.
XSLT Brent P. Christie Major USMC. XSLT Overview  What is XSLT? –XSL is the Extensible Style Language. –It has two parts: the transformation language.
Navigating XML. Overview  Xpath is a non-xml syntax to be used with XSLT and Xpointer. Its purpose according to the W3.org is  to address parts of an.
CSE3201/CSE4500 XPath. 2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.
TDDD43 XML and RDF Slides based on slides by Lena Strömbäck and Fang Wei-Kleiner 1.
SDPL 20075: Overview of XSLT1 5 Document Transformations n XSLT (1.0 W3C Rec. 11/1999; XSLT 2.0 Rec. 1/07) –A language for transforming XML documents –initial.
1/17 ITApplications XML Module Session 7: Introduction to XPath.
Introduction to XPath Web Engineering, SS 2007 Tomáš Pitner.
CSE3201/CSE4500 Information Retrieval Systems
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
XSLT and XPath, by Dr. Khalil1 XSL, XSLT and XPath Dr. Awad Khalil Computer Science Department AUC.
Representing Web Data: XML CSI 3140 WWW Structures, Techniques and Standards.
XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.
WORKING WITH XSLT AND XPATH
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
Representing Web Data: XML CSI 3140 WWW Structures, Techniques and Standards.
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
XPath Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Processing of structured documents Spring 2003, Part 7 Helena Ahonen-Myka.
XPath. Why XPath? Common syntax, semantics for [XSLT] [XPointer][XSLT] [XPointer] Used to address parts of an XML document Provides basic facilities for.
ECA 228 Internet/Intranet Design I XSLT Example. ECA 228 Internet/Intranet Design I 2 CSS Limitations cannot modify content cannot insert additional text.
CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "
XSLT part of XSL (Extensible Stylesheet Language) –includes also XPath and XSL Formatting Objects used to transform an XML document into: –another XML.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Lecture 11 XSL Transformations (part 1: Introduction)
August Chapter 6 - XPath & XPointer Learning XML by Erik T. Ray Slides were developed by Jack Davis College of Information Science and Technology.
XPath Aug ’10 – Dec ‘10. XPath   XML Path Language   Technology that allows to select a part or parts of an XML document to process   XPath was.
SDPL 2001Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. Nov-99) –A language for transforming XML documents »representative of features common.
1 XML Data Management XPath Principles Werner Nutt.
More XML XPATH, XSLT CS 431 – February 23, 2005 Carl Lagoze – Cornell University.
University of Nottingham School of Computer Science & Information Technology Introduction to XML 2. XSLT Tim Brailsford.
Martin Kruliš by Martin Kruliš (v1.1)1.
CSE3201/CSE4500 XPath. 2 XPath A locator for items in XML document. XPath expression gives direction of navigation.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
1 Technologies for an Information Age:.opennet Extensible Style Language—XSLT Fall Semester 2001 MW 5:00 pm - 6:20 pm CENTRAL (not Indiana) Time Bryan.
XSLT: How Do We Use It? Nancy Hallberg Nikki Massaro Kauffman.
XSLT I Robin Burke ECT 360. Outline History / Terminology XSLT processing XSLT syntax XPath XSLT basics Lab.
1 The XPath Language. 2 XPath Expressions Flexible notation for navigating around trees A basic technology that is widely used uniqueness and scope in.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
5 Copyright © 2004, Oracle. All rights reserved. Navigating XML Documents by Using XPath.
XML Schema – XSLT Week 8 Web site:
1 XSL Transformations (XSLT). 2 XSLT XSLT is a language for transforming XML documents into XHTML documents or to other XML documents. XSLT uses XPath.
Rendering XML Documents ©NIITeXtensible Markup Language/Lesson 5/Slide 1 of 46 Objectives In this session, you will learn to: * Define rendering * Identify.
CH 15 XSL Transformations 1. Objective What is XSL? Overview of XSL transformations Understanding XSL templates Computing the value of a node with xsl:value-of.
Unit 4 Representing Web Data: XML
Querying and Transforming XML Data
Chapter 7 Representing Web Data: XML
More XML XML schema, XPATH, XSLT
Presentation transcript:

SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –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 n Our goal: to understand the basic model and central features of the language –Overview and an example –Data model and processing model

SDPL 2003Notes 5: XSLT2 XSLT: Overview n 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 n 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 2003Notes 5: XSLT3 Overview of XSLT Transformation

SDPL 2003Notes 5: XSLT4 Style Sheets and Template Rules An xsl:stylesheet (or xsl:transform ) consists of template rules: An xsl:stylesheet (or xsl:transform ) consists of template rules: Template Template </xsl:template> n Rule applied to nodes of source tree matched by the Pattern –expressed using XML Path Language (XPath) n Template consists of »literal result tree fragments (elements, text), and »XSLT instructions for creating further result tree fragments conventionally used conventionally used as namespace prefix

SDPL 2003Notes 5: XSLT5 XPath in Short n W3C Recommendation (November 1999) –a compact non-XML syntax for addressing parts of XML documents –used also in other W3C languages » Specs for hyperlinks in XML : XLink (Rec.) and XPointer (Draft ) »XQuery (draft, 11/2002, for querying XML) –provides also typical primitives for manipulating strings, numbers and truth values

SDPL 2003Notes 5: XSLT6 An XSL transformation example n Transform below document to HTML: My Document My Document This is a short document. This is a short document. It only exists to demonstrate a It only exists to demonstrate a simple XML document. simple XML document. My Figure My Figure </doc>

SDPL 2003Notes 5: XSLT7 Example style sheet begins A Document A Document </xsl:template> </xsl:template>

SDPL 2003Notes 5: XSLT8 Example ( para s and emph s)

SDPL 2003Notes 5: XSLT9 Example ( figure s) Figure. Figure. </xsl:template>

SDPL 2003Notes 5: XSLT10 Example (end of style sheet) </xsl:stylesheet>

SDPL 2003Notes 5: XSLT11 Result (edited for readability) A Document A Document My Document My Document This is a short document. This is a short document. It only exists to demonstrate a simple XML document. It only exists to demonstrate a simple XML document. Figure 1. Figure 1. My Figure My Figure </BODY></HTML>

SDPL 2003Notes 5: XSLT12 What use of XSL or XSLT? n 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 2003Notes 5: XSLT13 XSLT in online document delivery n 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 1.0 (a new one exists but has to be installed separately) n XSLT in a Web server –an HTTP request for an XML document served by transforming XML on the fly to HTML (or other format) on the server

SDPL 2003Notes 5: XSLT14 Main Aspects of XSLT n Data model –How document data is viewed in XSLT? n Selection mechanism –How document parts are selected for processing? n Matching –How are the template rules selected? n Processing model –How XSLT execution proceeds?

SDPL 2003Notes 5: XSLT15 Data Model of XSLT and XPath n Documents viewed as abstract tree structures n Seven types of tree nodes –root (additional parent of document element) –element nodes –attribute nodes –text nodes –comments, processing instructions and namespaces NB: Entity references are expanded  no entity nodes NB: Entity references are expanded  no entity nodes

SDPL 2003Notes 5: XSLT16 XSLT/XPath document trees n Element nodes have elements, text nodes, comments and processing instructions of their (direct) content as their children –NB: attribute nodes are not considered children –the value of an element node is the concatenation of its text-node descendants n 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 2003Notes 5: XSLT17 XSLT/XPath Trees n 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: Example document: Written by the lecturer. Written by the lecturer.

SDPL 2003Notes 5: XSLT18 XSLT/XPath trees: Example Written by the lecturer. Written by the lecturer. 3 rd 5 th or 6 th 1 st 5 th or 6 th 7 th 4 th 2 nd

SDPL 2003Notes 5: XSLT19 Main Aspects of XSLT n Data model n Selection mechanism –How document parts are selected for processing? –A: With XPath expressions n Matching n Processing model

SDPL 2003Notes 5: XSLT20 XPath Expressions n 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 … n 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 2003Notes 5: XSLT21 Location paths Consist of location steps separated by ' / ' 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 »path begins with ‘ / ’  root is the first context node Complete form of a location step: AxisName :: NodeTest ( [ PredicateExpr ] )* 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 2003Notes 5: XSLT22 Location steps: Axes n In total 13 axes –for staying at the context node: »self –for going downwards: »child, descendant, descendant-or-self –for going upwards: »parent, ancestor, ancestor-or-self –for moving towards beginning or end of the document: »preceding-sibling, following-sibling, preceding, following –“Special” axes »attribute, namespace

SDPL 2003Notes 5: XSLT23 XPath Axes and Their Orientation Axes are oriented away from the context node (except attribute and namespace axes, which are unordered sets) Axes are oriented away from the context node (except attribute and namespace axes, which are unordered sets) –the position() for the closest node = 1 –for the most remote node, position() = last() The simplest axis, self: The simplest axis, self:

SDPL 2003Notes 5: XSLT24 XPath Axes and Their Orientation n parent:

SDPL 2003Notes 5: XSLT25 XPath Axes and Their Orientation n ancestor: n ancestor-or-self:

SDPL 2003Notes 5: XSLT26 XPath Axes and Their Orientation n child:

SDPL 2003Notes 5: XSLT27 XPath Axes and Their Orientation n descendant: n descendant-or-self:

SDPL 2003Notes 5: XSLT28 XPath Axes and Their Orientation n preceding-sibling: n following-sibling:

SDPL 2003Notes 5: XSLT29 XPath Axes and Their Orientation n following: n preceding:

SDPL 2003Notes 5: XSLT30 Location paths: Node tests n 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 »comment() : any comment node »processing-instruction() : any processing instruction node –node() : any node of any type

SDPL 2003Notes 5: XSLT31 Location paths: Abbreviations Abbreviations in location steps Abbreviations in location steps –'child::' can be omitted –'attribute::' can be shortened to ' –'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 ] ' –' /descendant-or-self::node()/' shortened to ' // '  Syntax resembles slightly Linux/Unix file path names

SDPL 2003Notes 5: XSLT32 Semantics of Location Paths (example) {2, 5, 7} AB context node C "txt"AB B C */node()/parent::B[child::A] */node()/parent::B[child::A] {3, 4, 6, 8} {2, 5, 7} final value: {2} value after each step:

SDPL 2003Notes 5: XSLT33 Location path examples (1) chap children of current node: chap children of current node:./chap (or equivalently: chap, or./child::node()[name()='chap'] ) The document element (child element of root node): /* The document element (child element of root node): /* Elements chapter anywhere (below the root): Elements chapter anywhere (below the root): //chapter (.//chapter  anywhere below the current node) All chapter s with attribute type="intro" : All chapter s with attribute type="intro" the previous chapter sibling : preceding-sibling::chapter[1] the previous chapter sibling : preceding-sibling::chapter[1]

SDPL 2003Notes 5: XSLT34 Location path examples (2) All child elements having an attribute type : All child elements having an attribute type NB: use of node sets as truth values: empty - false; non-empty - true All child elements of any author child: All child elements of any author child:author/* sect ions whose type attribute equals style attribute of the document element: sect ions whose type attribute equals style attribute of the document element: = First author child, and previous to the last: First author child, and previous to the last: author[1], author[last()-1]

SDPL 2003Notes 5: XSLT35 Location path examples (3) Predicate expressions can be Boolean combinations: Predicate expressions can be Boolean combinations: –child elements author with a publ child, but without degree or award children: author[publ and not(degree or award)] Closest chap ancestor (and highest): Closest chap ancestor (and highest): ancestor::chap[1] (ancestor::chap[last()]) author of the highest sect ancestor that is contained in an app endix: author of the highest sect ancestor that is contained in an app endix:ancestor::sect[ancestor::app][last()]/author

SDPL 2003Notes 5: XSLT36 Main Aspects of XSLT n Data model n Selection mechanism n Matching –How are the rules selected? –A: With Patterns n Processing model

SDPL 2003Notes 5: XSLT37 XSLT Patterns Main use in match attributes of template rules: Main use in match attributes of template rules: n Restricted location path expressions: –with steps using only child and attribute axes and separated by ' // ' or ' / ' »but arbitrary predicate expressions [ Expr ] allowed –may begin with id( 'IdVal' ) (for selecting element nodes by ID attribute values) –alternative patterns separated by ' | ' (node-set union operator)

SDPL 2003Notes 5: XSLT38 XSLT Patterns: Semantics A location path pattern P is of form Step 1  Step 2  … Step n-1  Step n, where each separator  is either ` / ´ or ` // ´ A location path pattern P is of form Step 1  Step 2  … Step n-1  Step n, where each separator  is either ` / ´ or ` // ´ –may also begin with ‘ / ’ ; Pattern ‘ / ’ matches only the root n Else P matches a node v n iff there are nodes v n,..., v 1 such that each v i satisfies the node test and possible predicates of Step i, and which form a path towards the root: –If P begins with a single ‘ / ’, node v 1 must be child of the root –in case of Step i-1 / Step i node v i-1 must be parent of v i –in case of Step i-1 // Step i node v i-1 may be any ancestor of v i

SDPL 2003Notes 5: XSLT39 XSLT Patterns: Examples match="sect-head | section/head" match="sect-head | section/head" –matches any element with name sect-head, and any head elements directly below a section A node matches the pattern /appendix//ulist/item[1] if it is the first item element in a ulist element which is contained in an appendix, which is the document element A node matches the pattern /appendix//ulist/item[1] if it is the first item element in a ulist element which is contained in an appendix, which is the document element

SDPL 2003Notes 5: XSLT40 Main Aspects of XSLT n Data model n Selection mechanism n Matching n Processing model –How does the XSLT execution proceed?

SDPL 2003Notes 5: XSLT41 XSLT Processing Model 0. Parse the document into a source tree 1. Construct the result tree by applying template rules to the source tree »may contain text and arbitrary elements (including XSL FO formatting objects) 2. Serialize the result tree (as XML, HTML or text), or interpret it to produce formatted output (in case of full XSL processing)

SDPL 2003Notes 5: XSLT42 Result Tree Construction Algorithm Start at the source tree root; while (unprocessed source nodes left) do Find matching template rules; Choose one rule (the one with highest priority); Instantiate rule 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;

SDPL 2003Notes 5: XSLT43 Selecting among multiple matching rules Priority of a rule can be specified explicitly: … Priority of a rule can be specified explicitly: … Default priorities based on the match pattern: Default priorities based on the match pattern: –0 for simple name tests (like ) –negative for less specific patterns e.g., node() –0.5 for more complex patterns n Multiple matching rules with the same maximum priority is an error - Processor may recover by (quietly!) choosing the last one of those rules

SDPL 2003Notes 5: XSLT44 Application of template rules Without a select attribute processes all children of current node Without a select attribute processes all children of current node –> Default behaviour: top-down traversal of source tree Otherwise the select ed nodes are processed Otherwise the select ed 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 Built-in rules allow recursive traversal to proceed gracefully in the absence of matching rules

SDPL 2003Notes 5: XSLT45 Default rules for elements and content Built-in rule for the root and element nodes: Built-in rule for the root and element nodes: Built-in rule for text and attribute nodes: Built-in rule for text and attribute nodes: Low priority  can be overridden Low priority  can be overridden

SDPL 2003Notes 5: XSLT46 A (Tricky) Processing Example Consider transforming document Consider transforming document b1 cc b2 dd b3 b1 cc b2 dd b3 with the below rules: </xsl:template> New: New: </xsl:template>

SDPL 2003Notes 5: XSLT47 Processing example (2) The result The result New: b1b3ccb2 New: b1b3ccb2 is obtained as follows: 1. Rule 1 matches the root node  Element node R is added to the result; Instruction selects the (only) C element for processing (which will produce the contents of node R ). 2. Rule 2 with pattern "C" creates into result tree a NewC element node with text node " New: " as its first child.

SDPL 2003Notes 5: XSLT48 Processing example (3) 3. Instruction selects element B siblings of current node ( C ). The built-in element rule applies to these, and the built-in text rule to their children. Result: text nodes " b1 " and " b3 " become the next children of NewB. 4. Instruction in the context of element node C selects its children, " cc" and b2, for processing. The built-in text rule inserts value " cc" to the result tree, and the B element node becomes "b2" in the result (similarly to step 3).

SDPL 2003Notes 5: XSLT49 Processing example (4) A B B B C D b1 Source Result cc b2 dd R NewC New: b b3b1 cc b2

SDPL 2003Notes 5: XSLT50 Is it Really So Tricky? n In practice: Seldom –A computer scientist wants to understand how a model really works … n XSLT is high-level declarative language for describing transformations –Normally no need to think so carefully about execution; Often sufficient just to specify declarative rules to handle different cases, like –Normally no need to think so carefully about execution; Often sufficient just to specify declarative rules to handle different cases, like