1 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Transform XML Documents Roger L. Costello XML Technologies.

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

1 Roger L. Costello 16 June 2010 XQuery
1 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath Roger L. Costello (XML Technologies) With changes and additions by.
1 The MITRE Using XSL to Generate XHTML Documents Roger L. Costello XML Technologies.
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.
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
XSL XSLT and XPath 11-Apr-17.
+ XSL eXtensible Stylesheet Language. + 2 XML Lecture Adapted from the work of Prof Mark Baker ACET, University of Reading.
XML and XSL Transforming your XML documents with eXtensible Stylesheet Language Transformations [Optional Lecture]
XML Name spaces. Different people may invent similar tag names Here is an XML element describing a piece of furniture: table99 dining table Here.
1 CP3024 Lecture 9 XML revisited, XSL, XSLT, XPath, XSL Formatting Objects.
XSLT Stylesheets Some more advanced examples (adapted from the Edinburgh LT site)
Introduction to XSLT Transparency No. 1 Introduction to XSLT Cheng-Chia Chen.
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 Concepts Lecture 7. XML Display Options What can XSL Transformations do? generation of constant text suppression of content moving text (e.g., exchanging.
XSL Unit 6 November 2. XSL –eXtensible Stylesheet Language –Basically a stylesheet for XML documents XSL has three parts: –XSLT –XPath –XSL-FO.
Introduction to XSLT Transparency No. 1 Introduction to XSLT Cheng-Chia Chen.
1 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Transform XML Documents Roger L. Costello XML Technologies.
XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
1 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Enhance HTML Documents Roger L. Costello XML Technologies.
1 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Transform XML Documents into Text Files Roger L. Costello XML Technologies.
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
1 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Using Extension Elements and Extension Functions with XSLT and XPath Roger L. Costello.
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
17 Apr 2002 XML Stylesheets Andy Clark. What Is It? Extensible Stylesheet Language (XSL) Language for document transformation – Transformation (XSLT)
1 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Using Extension Elements and Extension Functions with XSLT and XPath Roger L. Costello.
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 Part 3B. id() The id() function returns a node-set containing the node or nodes with a given ID attribute. An ID attribute in this context is any.
XSLT 1. C ONTENTS XSLT and uses XSLT example Simple XML How XSLT works? Template-match Value-of For-each and sort If Choose-when Apply-templates Where.
Sheet 1XML Technology in E-Commerce 2001Lecture 6 XML Technology in E-Commerce Lecture 6 XPointer, XSLT.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
XSL XML Stylesheet Langauage. XPath Notation for addressing elements in an XML document /xyz - selects the root element /xyz/abc - selects all elements.
CIS 451: XSL Dr. Ralph Westfall February, Problems With XML no formatting capabilities contra formatting tags like, etc. in HTML CSS can be used.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
CSE3201/CSE4500 Information Retrieval Systems XSLT – Part 2.
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 I Robin Burke ECT 360. Outline XSLT processing XSLT syntax XPath XSLT basics Lab.
XSLT part of XSL (Extensible Stylesheet Language) –includes also XPath and XSL Formatting Objects used to transform an XML document into: –another XML.
The Information School at the University of Washington INFO 440: Information System Design Fall 2003 * Bob Boiko * MSIM Associate Chair XML Transforms.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Transforming Documents „a how-to of transforming xml documents“ Lecture on Walter Kriha.
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.
Lecture 15 XSL Transformations (part V) Acknowledgment:
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Applying eXtensible Style Sheets (XSL) Ellen Pearlman Eileen Mullin Programming.
XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014.
1 Overview of XSL. 2 Outline We will use Roger Costello’s tutorial The purpose of this presentation is  To give a quick overview of XSL  To describe.
XP New Perspectives on XML, 2 nd Edition Tutorial 8 1 TUTORIAL 8 CREATING ELEMENT GROUPS.
More XML XPATH, XSLT CS 431 – February 23, 2005 Carl Lagoze – Cornell University.
 XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents.
CSE3201/CSE4500 Information Retrieval Systems XSLT – Part 2.
University of Nottingham School of Computer Science & Information Technology Introduction to XML 2. XSLT Tim Brailsford.
ACG 4401 XSLT Extensible Stylesheet Language for Transformations Presenting XML and XBRL.
Designing Streamable XPath Expressions Roger L. Costello January 5,
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 Whitespace Handling Roger L. Costello XML Technologies.
ACG 4401 XSLT Extensible Stylesheet Language for Transformations Presenting XML and XBRL.
© 2013 The MITRE Corporation. All rights reserved. XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello, February 3, 2014.
1999 costello1 XSL by Example (part 2) Roger L. Costello
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.
Using XSLT and XPath to Transform XML Documents
Using Extension Elements and Extension Functions with XSLT and XPath
Presentation transcript:

1 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Transform XML Documents Roger L. Costello XML Technologies

2 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Note All the xsl functionality that we learned in creating XSL-enhanced HTML documents are applicable in transforming XML documents

3 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Transformation Language XSL may be used as a transformation language --> it may be used to transform an XML document into another XML document (perhaps the new one is the same, minus company sensitive data) Transformation Engine (XSL Processor) XSL XML

4 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Example: Filter Gold Members Jeff lightgrey David lightblue Roger lightyellow Jeff lightgrey Roger lightyellow

5 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. XML Transformations - all about (Template) “Rules” “Hey xsl processor, when you encounter the root element (e.g., FitnessCenter) do [action1]” “Hey xsl processor, when you encounter the Member element do [action2]” “Hey xsl processor, when you encounter the Name element do [action3]” And so forth

6 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. XML Transformations - all about (Template) “Rules” Each template rule has two parts: –A pattern or matching part, that identifies the XML node in the source document to which the action part is to be applied. Matching information is contained in an attribute. –An action part that details the transformation of the node

7 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. XSL Document Structure [action] [action] [action]...

8 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Template Rules Template rules take the following general form: [ action ]

9 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Template Rules (Example) “Hey XSL processor, as you parse through the XML document and you get to a element use this template rule.” “Go to each of my children (the Member children) and apply the template rules to them.”

10 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Terminology In FitnessCenter.xml we have (snippet): Jeff lightgrey... “Member is a child element of the FitnessCenter element. Name, Phone, Phone, and FavoriteColor are children elements of the Member element. Member is a parent of Name. FitnessCenter and Member are ancestors of Name.”

11 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. xsl:element Suppose that you are writing a stylesheet to generate an XML document. Obviously, you will need your stylesheet to output elements. –xsl:element is used to create elements [contents of the new element] [contents of the new element] creates

12 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Identity Transformation For our first example, lets create a stylesheet which simply creates an XML document that is a copy of the input XML document

13 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Document / PI Element FitnessCenter Element Member Element Member Element Member Element Name Element Phone Element Phone Element FavoriteColor... Text Jeff Text Text Text lightgrey...

14 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl=" version="1.0"> Cont. -->

15 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. (see xml-example01)

16 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Jeff lightgrey David lightblue Roger lightyellow Note that we've lost the attribute on the Member element

17 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. For each attribute Add an attribute to the element being output. The name of the attribute is the name of the current attribute being processed. The value of the attribute is the value of the current attribute being processed. Getting Member’s Attribute: (see xml-example02)

18 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Jeff lightgrey David lightblue Roger lightyellow

19 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Generalize Our identity stylesheet will only work for FitnessCenter XML documents. We can make a stylesheet which does an identity transformation on any XML document.

20 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl=" version="1.0"> (see xml-example03)

21 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Default Template Rules Every xsl document has two default template rules These rules are applied when the XSL Processor cannot find a template rule to use in your stylesheet Here are the two default template rules: “Match on the document or any element. The action is to go to the children and execute their template rules.” “Match on a text node. The action is to output the value of the text node.”

22 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Multiple Applicable Rules Suppose that the XSL Processor is processing FitnessCenter and it gets to the element. Why does it use:... and not the default template rule:... ??? After all, both apply. Answer: given two rules that apply, the more specific rule wins. --> Clearly, “*” is much more general than “Member”. “*” matches on any element. “Member” just matches on the Member element.

23 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Smallest Identity Transformation Stylesheet Now that we know about the default template rules, we can further reduce the size of the stylesheet.

24 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl=" version="1.0"> (see xml-example04)

25 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. The xsl:apply-templates element (without the select attribute) tells the XSL Processor to apply the template rules to all children (in document order) The xsl: apply-templates element can have a select attribute that tells the XSL Processor to process only the child element that matches “pattern”. –Thus, the select attribute rule enables us to specify the order in which the children are processed

26 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. "Go to the template rule for my Name child element. Then go to the template rule for the work Phone child element." "Go to all the child element nodes (not to any child text nodes)." Do Lab4, Part 1-4

27 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Any Difference?

28 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Can we use anything other that name(.)?

29 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. The XSL Vocabulary if stylesheet for-each choose apply-templates template value-of xsl: An XSL Processor is a piece of software which understands the semantics of this vocabulary

30 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Name of Prefix - Irrelevant! if stylesheet for-each choose apply-templates template value-of xsl: We can call this anything we want

31 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. mode Attribute Allows you to create multiple template rules for the same element. Each template rule can process the element differently. So, you can have multiple template rules for the same element. Just give each template rule a different mode

32 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Problem Identity transform the FitnessCenter.xml document. However, after you have copied all the Members, follow up with a (new) GoldMembers section, containing the name of each gold member (within stars) The next slide shows what the output XML file should look like

33 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Jeff lightgrey David lightblue Roger lightyellow ***David*** (see xml-example05) Note that the names here are processed differently than the name in the GoldMembers section

34 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl=" version="1.0">

35 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. *** *** Do Lab5, Part 1

36 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Stylesheet Reuse via xsl:include and xsl:import The elements xsl:include and xsl:import enable you to reuse other stylesheets. These elements are “top-level elements”. This means that they must be immediate children of the xsl:stylesheet element (i.e., they cannot be within a template rule) The xsl:include element is basically a macro substitution - the element is replaced by the contents of stylesheet it references

37 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:stylesheet xmlns:xsl=" version="1.0"> Replace the xsl:include element with the contents of the referenced stylesheet (i.e., all the children of xsl:stylesheet) toUpperCase.xsl

38 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. xsl:import xsl:import acts just like xsl:include - the stylesheet that it references is macro-substituted. However, there is a difference: –With xsl:include the stuff that is macro-substituted into the stylesheet has the same precedence as the rest of the stylesheet. It is as though you had one stylesheet. –With xsl:import the stuff that is macro-substituted into the stylesheet has lower precedence than the rest of the stylesheet. Also, all xsl:import elements must come first in the stylesheet.

39 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. Pipelining Stylesheets Using the XSL API (TRaX - Transformation API for XML) I have created a program which enables you to have a pipeline of stylesheets, i.e., the output of stylesheet[i] is the input to stylesheet[i+1] Example: XML FitnessCenter.xml XSL generateNameList.xsl XSL toUpperCase.xsl List of Member names, in upper case! See xml-example06. Type this at the DOS prompt: run-xalan FitnessCenter.xml generateNameList.xsl toUpperCase.xsl

40 Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. xml-example06 Here's how to use the program that I created: run-xalan … That is, you can specify any number of xsl files to be used in the pipeline.