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.

Slides:



Advertisements
Similar presentations
XML-XSL Introduction SHIJU RAJAN SHIJU RAJAN Outline Brief Overview Brief Overview What is XML? What is XML? Well Formed XML Well Formed XML Tag Name.
Advertisements

Advanced XSLT. Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another.
Advanced XSLT II. Iteration in XSLT we sometimes wish to apply the same transform to a set of nodes we iterate through a node set the node set is defined.
CG0119 Web Database Systems Parsing XML: using SimpleXML & XSLT.
 2001 Prentice Hall, Inc. All rights reserved. 1.
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
XSLT (eXtensible Stylesheet Language Transformation) 1.
Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.
+ XSL eXtensible Stylesheet Language. + 2 XML Lecture Adapted from the work of Prof Mark Baker ACET, University of Reading.
XSLT Stylesheets Some more advanced examples (adapted from the Edinburgh LT site)
Rendering XML documents with XSL The most powerful approaches to rendering XML documents involve using XSL (eXtensible Stylesheet Language) XSL enables.
XSL Transformations (XSLT) Meghasyam Bokam April’1 st, 2002.
XSL Unit 6 November 2. XSL –eXtensible Stylesheet Language –Basically a stylesheet for XML documents XSL has three parts: –XSLT –XPath –XSL-FO.
XML Why is XML so popular? –It’s flexible –It enables you to create your own documents with elements (tags) that you define Non-XML example: This is a.
XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
CS 4408 Lecture 9. XSLT instruction elements We have seen that a template can contain non-XSLT text, canned text that it inserts into the result tree.
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
Object Oriented Programming III1 XSLT Processing XML using XSLT Using XPath.
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
MC 365 – Software Engineering Presented by: John Ristuccia Shawn Posts Ndi Sampson XSLT Introduction BCi.
Manohar – Why XML is Required Problem: We want to save the data and retrieve it further or to transfer over the network. This.
SD2520 Databases using XML and JQuery
Using XSLT and XPath to Enhance HTML Documents Reference: Roger L. Costello
XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain.
ECA 228 Internet/Intranet Design I Intro to XSL. ECA 228 Internet/Intranet Design I XSL basics W3C standards for stylesheets – CSS – XSL: Extensible Markup.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
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.
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
Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall XML Transformation: XSLT Semantic Web - Spring 2007 Computer.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL.
Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:
XML About XML Things to be known Related Technologies XML DOC Structure Exploring XML.
1 XSLT An Introduction. 2 XSLT XSLT (extensible Stylesheet Language:Transformations) is a language primarily designed for transforming the structure of.
ECA 228 Internet/Intranet Design I XSLT Example. ECA 228 Internet/Intranet Design I 2 CSS Limitations cannot modify content cannot insert additional text.
Li Tak Sing COMPS311F. XPath XPath is a simple language that allows you to write expressions to refer to different parts of an XML document. We will learn.
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)
 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 – XSL: Extensible Stylesheet Language Transformations (XSLT) Outline 12.1Introduction 12.2Setup.
XSL and XSLT 06. XSL and XSLT © Aptech Limited Introduction to XSL  In this first lesson, Introduction to XSL, you will learn to:  Define XSL, XSLT,
ITB Web programming for E- Commerce 1 ITB6227 Programming for E-COMMERCE Lecture Presentation of XML Documents.
XML – Part III. The Element … This type of element either has the element content or the mixed content (child element and data) The attributes of the.
[ Part III of The XML seminar ] Presenter: Xiaogeng Zhao A Introduction of XQL.
XP New Perspectives on XML, 2 nd Edition Tutorial 8 1 TUTORIAL 8 CREATING ELEMENT GROUPS.
XSLT. XSLT stands for Extensible Stylesheet Language Transformations XSLT is used to transform XML documents into other kinds of documents. XSLT can produce.
Unit 3 — Advanced Internet Technologies Lesson 11 — Introduction to XSL.
XPath. XPath, the XML Path Language, is a query language for selecting nodes from an XML document. The XPath language is based on a tree representation.
More XML XPATH, XSLT CS 431 – February 23, 2005 Carl Lagoze – Cornell University.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
University of Nottingham School of Computer Science & Information Technology Introduction to XML 2. XSLT Tim Brailsford.
XP 1 XSLT II Robin Burke ECT 360. XP 2 Outline Conditionals Numbering Functions and operators Variables and parameters Named and recursive templates.
XP 1 XSLT II Robin Burke ECT 360. XP 2 Homework #4 Forgot we hadn't covered "choose" becomes extra credit My books.xml didn't include descriptions.
XSLT: How Do We Use It? Nancy Hallberg Nikki Massaro Kauffman.
Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall XML Transformation: XSLT Semantic Web - Fall 2005 Computer.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
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.
XSLT, XML Schema, and XPath Matt McClelland. Introduction XML Schema ▫Defines the content and structure of XML data. XSLT ▫Used to transform XML documents.
Rendering XML Documents ©NIITeXtensible Markup Language/Lesson 5/Slide 1 of 46 Objectives In this session, you will learn to: * Define rendering * Identify.
XSLT: The XML transformation language
XSL Transformations (XSLT)
XSLT 1.0.
XML WITH CSS.
CS 431 – February 28, 2005 Carl Lagoze – Cornell University
WHAT IS XSLT?.
More XML XML schema, XPATH, XSLT
Presentation transcript:

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 Processing multiple elements with xsl:foreach Matching and selecting nodes with patterns Understanding the default template rules 2

Objectives cont.. Attribute value templates Deciding which output to include Counting nodes Sorting output Modes Output methods 3

What Is XSL? XSL stand for Extensible Stylesheet Language It include both transformation language and a formatting language.  The Transformation - provides elements that define rules for how one XML document is transformed into another XML document  Formatting - ????? 4

Overview of XSL Transformations In an XSL transformation, an XSLT processor reads both an XML document and an XSLT style sheet XSLT is designed primarily for XML-to-XML and XML-to-HTML transformations root  Trees - A tree is a data structure composed of connected nodes beginning with a top node called the root 5

Overview of XSL Transformations cont… One useful property of a tree is that each node and its children also form a tree XSLT processors model an XML document as a tree that contains seven kinds of nodes ✦ The root ✦ Elements ✦ Text ✦ Attributes ✦ Namespaces ✦ Processing instructions ✦ Comments 6

Overview of XSL Transformations cont… Unlike CSS, XSL is not limited to working only with whole elements XSLT operates by transforming one XML tree into another XML tree XSLT cannot transform from non-XML formats such as PDF, TeX, Microsoft Word, PostScript, MIDI, or others XSLT can work with HTML XSLT is not a general-purpose language for transforming arbitrary data 7

Overview of XSL Transformations cont…  XSLT style sheet documents template rules An XSLT document contains template rules A template rule has a pattern specifying the nodes it matches A template to be instantiated and output when the pattern is matched XSLT processor transforms an XML document under the control of an XSLT style sheet, it walks the XML document tree starting at the root, and following an order defined by the template rules 8

Overview of XSL Transformations cont… xsl:template Each template rule is an xsl:template element match xsl:template The pattern of the rule is placed in the match attribute of the xsl:template element All instructions in the template for doing things, such as selecting parts of the input tree to include in the output tree, are performed by XSLT elements 9

Overview of XSL Transformations cont… 10

Overview of XSL Transformations cont… Where does the XML transformation happen? 1. The XML document and associated style sheet are both served to the client(web browser), which then transforms the document as specified by the style sheet and presents it to the user 2. The server applies an XSLT style sheet to an XML document to transform it to some other format (generally HTML) and sends the transformed document to the client (web browser) 3. A third program transforms the original XML document into some other format(often HTML) before the document is placed on the server. Both server and client only deal with the transformed document 11

XSL templates xsl:template Template rules defined by xsl:template elements are the most important part of an XSLT style sheet This example below match the root node of the input tree 12

XSL templates cont… xsl:apply-templates The xsl:apply-templates element An xsl:apply-templates element tells the processor to compare each child node of the matched source element against the templates in the style sheet If a match is found, output the template for the matched node Review page

XSL templates  The select attribute To choose a particular set of children instead of all children, supply xsl:apply-templates with a select attribute designating the children to be selected, as in this template rule Example atalog&xsltfile=cdcatalog_apply 14

XSL templates The select attribute uses the same kind of patterns as the match attribute of the xsl:template element If no select attribute is present, all child element, text, comment, and processing instruction nodes are selected. 15

Computing the Value of a Node with xsl:value-of xsl:value-of The xsl:value-of element computes the string value of something Example 16

Processing Multiple Elements with xsl:for-each There are two ways of processing multiple elements in turn First method xsl:apply-templatesselect Simple use xsl:apply-templates with a select attribute The select=”.” in the second template tells the formatter to take the value of the matched node, ATOM 17 OR

Processing Multiple Elements with xsl:for-each xsl:for-each Second method is xsl:for-each xsl:for-each element processes each element chosen by its select attribute in turn This is useful when you need to format the same content differently in different places in the style sheet 18

Patterns for Matching Nodes The match attribute of the xsl:template element supports a complex syntax that allows you to indicate precisely which nodes you do and do not want to match  Matching the root node XSLT style sheets generally start with a rule that applies to the root node 19

Patterns for Matching Nodes This rule applies to the root node and only the root node of the input tree  Matching element names This template matches ATOM elements and makes their ATOMIC_NUMBER children bold 20

Patterns for Matching Nodes  Wildcards (*) Sometimes you want a single template to apply to more than one element Use (*) to indicate that a template matches all elements This above template says that all input elements should be wrapped in a P element 21 xsl:template match=”*”> xsl:template match=”*”>

Patterns for Matching Nodes  Matching children with / You can use the / symbol to match hierarchies of elements / symbol root Alone, the / symbol refers to the root node Between two names it indicates that the second is the child of the first E.g. / ATOM/NAME refers to NAME elements that are children of ATOM elements 22

Patterns for Matching Nodes This rule selects SYMBOL elements that are children of ATOM elements. Read it backward. The. in refers to the SYMBOL and not to the ATOM 23

Patterns for Matching Nodes You can specify deeper matches by stringing patterns together. E.g. PERIODIC_TABLE/ATOM/NAME PERIODIC_TABLE/ATOM/NAME selects NAME elements whose parent is an ATOM element whose parent is a PERIODIC_TABLE element * You can also use the * wildcard to substitute for an arbitrary element name in a hierarchy 24

Patterns for Matching Nodes This template rule applies to all SYMBOL elements that are grandchildren of a PERIODIC_TABLE element 25

Patterns for Matching Nodes  Matching descendants with // Bypass intermediate nodes and simply select all the elements of a given type This template rule applies to all NAME descendants of PERIODIC_TABLE, no matter how deep 26 // //

Patterns for Matching Nodes  Matching by ID You might want to apply a particular style to a particular single element without changing all other elements of that type 27

Patterns for Matching Nodes  Matching attributes see Listing 15-7 for example This template above rule matches UNITS attributes, and wraps them in an I element 28

Attribute Value Templates Attribute value templates enable a style sheet to determine the content of an attribute dynamically based on the content of the input document rather than using a literal fixed value in the style sheet 29

Attribute Value Templates cont… How would you extract the content of the elements and the value of the attributes? 30 ” ATOMIC_WEIGHT=” ” ATOMIC_NUMBER=” ” /> ” ATOMIC_WEIGHT=” ” ATOMIC_NUMBER=” ” /> Wrong !!! The < character is not allowed in an attribute value

Attribute Value Templates cont… curly braces {} Instead, inside attribute values, data enclosed in curly braces {}, takes the place of the xsl:value-of element 31 ATOMIC_WEIGHT=”{ATOMIC_WEIGHT}” ATOMIC_NUMBER=”{ATOMIC_NUMBER}” /> The correct way to write the preceding template rule is like this

Attribute Value Templates cont… In the previous slide, In the output, {NAME} is replaced by the value of the NAME child element of the matched ATOM {ATOMIC_WEIGHT} is replaced by the value of the ATOMIC_WEIGHT child element of the matched ATOM {ATOMIC_NUMBER} is replaced by the value of the ATOMIC_NUMBER child element 32

Counting Nodes with xsl:number The xsl:number element inserts a formatted integer into the output document The value of the integer is given by the value attribute See Listing

Counting Nodes with xsl:number cont… Number to string conversion xsl:number: These are all adjustable through four attributes of xsl:number: ✦ format ✦ letter-value ✦ grouping-separator ✦ grouping-size 34

Counting Nodes with xsl:number cont… xsl:number The format attribute - You can adjust the numbering style used by xsl:number using the format attribute ✦ i—The lowercase Roman numerals i, ii, iii, iv, v, vi,... ✦ I—The uppercase Roman numerals I, II, III, IV, V, VI,... ✦ a—The lowercase letters a, b, c, d, e, f,... ✦ A—The uppercase letters A, B, C, D, E, F,... 35

Counting Nodes with xsl:number cont… Using xsl:format with number This rule numbers the atoms with capital Roman numerals You can specify decimal numbering with leading zeros by including the number of leading zeros you want in the format attribute. E.g. format=“01”. This will produces the sequence 01,02,03 36

Counting Nodes with xsl:number cont… Using letter-value with xsl:number The letter-value attribute distinguishes between letters interpreted as numbers and letters interpreted as letters. E.g. 37 For instance, if you want to use format=”I” to start the sequence I, J, K, L, M, N,... instead of I, II, III, IV, V, VI,..., you would set the letter-value attribute to the keyword alphabetic

Sorting Output The xsl:sort element sorts the output nodes into a different order than they were generated in xsl:sort element appears as a child of an xsl:apply-templates element or xsl:for-each element See Listing Order of the sort from the default ascending order to descending by setting the order attribute to descending

Making Choices XSLT provides two elements that allow you to choose different output based on the input The xsl:if element either does or does not output a given fragment of XML depending on what patterns are present in the input The xsl:choose element 39

Making Choices cont… The xsl:if element provides a simple facility for changing the output The test attribute of xsl:if contains an expression that evaluates to a boolean There are no xsl:else or xsl:else-if elements 40,,

Making Choices cont… xsl:choose The xsl:choose element selects one of several possible outputs depending on several possible conditions xsl:when Each condition and its associated output template is provided by an xsl:when child element 41

Making Choices cont… 42 This rule changes the color of the output based on whether the STATE attribute of the ATOM element is SOLID, LIQUID, or GAS

Output  Document Type Declaration XSLT does not provide any elements for building a DTD for the output document with,,, and declarations 43

Output  CDATA sections XSLT does not allow you to insert CDATA sections at arbitrary locations in XML documents produced by XSL transformations 44

references XSL Tutorial 45