XML Schema – XSLT Week 8 Web site:

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

XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents.
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.
XSL eXtensible Stylesheet Language. What is XSL? XSL is a language that allows one to describe a browser how to process an XML file. XSL can convert an.
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.
1 CP3024 Lecture 9 XML revisited, XSL, XSLT, XPath, XSL Formatting Objects.
XSLT Stylesheets Some more advanced examples (adapted from the Edinburgh LT site)
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 Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
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.
Object Oriented Programming III1 XSLT Processing XML using XSLT Using XPath.
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)
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
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.
Chapter 12 Creating and Using XML Documents HTML5 AND CSS Seventh Edition.
CSE3201/CSE4500 XPath. 2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.
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
Extensible Markup and Beyond
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:
ECA 228 Internet/Intranet Design I XSLT Example. ECA 228 Internet/Intranet Design I 2 CSS Limitations cannot modify content cannot insert additional text.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "
Extensible Stylesheet Language Chao-Hsien Chu, Ph.D. School of Information Sciences and Technology The Pennsylvania State University XSL-FO XSLT.
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.
Transforming Documents „a how-to of transforming xml documents“ Lecture on Walter Kriha.
CIS 375—Web App Dev II XSL. 2 XSL Introduction XSL stands for _____________________________. XSL is the language used for manipulating and displaying.
Lecture 11 XSL Transformations (part 1: Introduction)
Waqas Anwar Next SlidePrevious Slide. Waqas Anwar Next SlidePrevious Slide XML XML stands for EXtensible Markup Language.
1 Introduction  Extensible Markup Language (XML) –Uses tags to describe the structure of a document –Simplifies the process of sharing information –Extensible.
 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 – XSL: Extensible Stylesheet Language Transformations (XSLT) Outline 12.1Introduction 12.2Setup.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Applying eXtensible Style Sheets (XSL) Ellen Pearlman Eileen Mullin Programming.
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,
ACG 6415 XSLT Presenting XML and XBRL. Re-Purpose  The main benefit of XML / XBRL Reusability of Data contained in Instance Document We need a method.
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.
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.
Dr. Chunbo Chu Week 3. XML Not a replacement for HTML. XML and HTML were designed with different goals: XML was designed to transport and store data,
XSLT. XSLT stands for Extensible Stylesheet Language Transformations XSLT is used to transform XML documents into other kinds of documents. XSLT can produce.
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.
 XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
ACG 4401 XSLT Extensible Stylesheet Language for Transformations Presenting XML and XBRL.
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.
ACG 4401 XSLT Extensible Stylesheet Language for Transformations Presenting XML and XBRL.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
XML Extensible Markup Language
Displaying Data with XSLT ©NIITeXtensible Markup Language/Lesson 6/Slide 1 of 45 Objectives In this lesson, you will learn to: * Perform conditional formatting.
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.
1 XSLT XSLT (extensible stylesheet language – transforms ) is another language to process XML documents. Originally intended as a presentation language:
XSLT: The XML transformation language
ACG 4401 XSLT Extensible Stylesheet Language for Transformations
Applying eXtensible Style Sheets (XSL)
XML WITH CSS.
Presentation transcript:

XML Schema – XSLT Week 8 Web site:

XSLT The details for formatting XML documents was originally in a specification called XSL, which stands for eXtensible Style Language. However, because it was taking so long to finish, the W3C divided XSL into two pieces: XSLT (for Transformations) and XSL-FO (for Formatting Objects). XSLT is used to transform XML documents. The end result might be another XML document or an HTML document. In reality, you can transform an XML document into practically any document type you like. Transforming an XML document means using XSLT to analyze its contents and then take certain actions depending on what elements are found. You can use XSLT to reorder the output according to specific criteria, display only certain pieces of information, and much more. XSL-FO is typically used to format XML for print output, such as going directly to a PDF. It is not supported by any browsers, and requires specific parsing software to use.

Transforming XML with XSLT Let’s start with an overview of the transformation process. The process starts with two documents, the XML document which contains the source data to be transformed, and the XSLT style sheet document which describes the rules of the transformation. While you can transform XML into nearly any format, we are going to use examples that return HTML. To perform the actual transformation, you will need an XSLT processor, or a browser that supports XSLT. Most current XML Editor have built-in XSLT support, as do most current Web browsers.

Analyzing the source XML To begin, you will need to link your XML document to your XSLT style sheet using the xml-stylesheet processing instruction. Then, when you open your XML document in an XSLT processor or a browser, the instruction tells the processor to perform the XSLT transformation before displaying the document. In the first step of this transformation, the XSLT processor analyze the XML document and converts it into a node tree. A node tree is a hierarchical representation of the XML document. In the tree, a node is one individual piece of the XML document (such as an element, an attribute, or some text content).

Assessing the XSLT Style Sheet Once the processor has identified the nodes in the source XML, it then looks to an XSLT style sheet for instructions on what to do with those nodes. Those instructions are contained in templates which are comparable to functions in a programming language. Each XSLT templates has two parts: first, a label that identifies the nodes in the XML document to which the template applies (select="customer/name"); and second, instructions about the actual transformation that should take place (xsl:value-of). The instructions or rules, will either output or further process the nodes in the source document. They can also contain literal elements that should be output as is ( XSLT sample …).

Performing the Transformation The XSLT transformation begins by processing the root template. Every XSLT style sheet must have a root template; this is the template that applies to the source XML document’s root node. The root template is defined with. Within this root template, there may be other sub-templates which can then apply to other nodes in the XML document. And the transformation continues until the last instruction of the root template is processed. The transformation document is then either saved to another file, displayed in a browser or both. NOTE: XSLT style sheets are text files and are saved with an.xsl extension. XSLT uses the XPath language to identify nodes. XPath Patterns and Expression, and XPath Functions will be discussed later.

XML with XSLT John Smith true

XSL XSLT sample XSLT Sample Customer: has credit limit of:

Sample XSLT 01_xslt_sample.xml 01_xslt_sample.xsl

XSLT Style Sheet 1)Type to indicate that the XSLT style sheet is an XML document. 2)Type to specify the namespace for the style sheet and declare its prefix (xsl). 3)Create the root template. 4)Set the processor’s output method to HTML. 5)Output a node’s content. 6)Finally, type to complete the style sheet.

To Create the Root Template 1)Type <xsl:template. 2)Then, type match="/". The forward slash matches the root node of the XML source document. 3)Next, type > to close the tag. 4)Specifying the rules of the template, that is, what transformation should happen with the XML document. 5)Finally, type to complete the root template.

To Set the Processor’s Output Method to HTML 1)Immediately after the xsl:stylesheet element, type <xs:output. 2)Then, type method="html" />. To add HTML to any template’s output: 1)Inside any other template rule, that is, between and its matching, add the HTML that you would like to output when this particular template is applied.

To Output a Node’s Content 1)If desired, create the HTML code that will format the content. 2)Type <xsl:value-of. 3)Then, type select="expression", where expression identifies the node set from the XML source document whose content should be output at this point. 4)Finally, type /> to close the tag. NOTE: You can use select="." to output the content of the current node. If the select expression matches more than one node in the XML document, only the first node’s value is output. If the select expression matches a node, the string value of that node (the text that node contains) is output. If the node has child elements, the output includes the text contained in those child elements as well. If the select expression matches a node set that is empty, there is nothing to output. If the select expression evaluates to a number, the number is converted to a string for output. If the select expression evaluates to a Boolean expression (evaluates to either true or false), the output will be either the text “true” or the text “false”.

Looping Over Nodes The xsl:value-of element will only act on one node, even if there are many nodes that it matches. The xsl:for-each element allows you act on all nodes matched. It processes all the nodes matched by its select attribute, one after the other.

To Process All Matched Nodes 1)Within a template rule, type <xsl:for-each. 2)Then, type select="expression", where expression identifies the set of nodes that will be processed. 3)Next, type > to close the tag. 4)Specify what processing should take place. 5)Finally, type to complete the instruction. NOTE: In general, place the xsl:for-each right before the rules that should be repeated for each node found. To add a table or some other container, you would do so before and after the opening and closing tags, respectively. The xsl:for-each element is often used to create HTML tables. Place the opening and closing tags before and after the. Then, place the and tags as part of the processing that should take place as described in Step 4 above. Because an XSLT style sheet is also an XML document itself, when HTML is part of an XSLT file, it must follow XML’s rules. For example, every opening tag must have a matching closing tag and elements may not overlap. In the select condition of the xsl:for-each element, you can require a specific attribute match by

XSL – to process all matched nodes XSLT Loop Over Nodes Name Phone

(loop) Process All Matched Nodes 02_xslt_loop.xml 02_xslt_loop.xsl

Processing Nodes Conditionally It is not common to want to process a node or a set of nodes only if a certain condition is met. The condition is written as an expression. To process nodes conditionally: 1)Within a template rule, type <xsl:if. 2)Then, type test="expression", where expression specifies a node set, a string, or a number. 3)Next, type > to close the tag. 4)Specify what should happen if the node set, string, or number specified in Step 2 is not empty (or not equal to zero, in the case of a number). 5)Finally, type to complete the instruction. NOTE: When referring to a node set in the expression. The test returns true if the node set is not empty; that is, if it contains at least one node. If you want to be able to specify an alternate result when the expression is false, e.g. an else condition, use xsl:choose.

Comparison Operators supported by XPath OperatorDescriptionExample = Tests whether two values are equal to each ='abc' !=Tests whether two values are !='abc' <Tests whether one value is less than < 'abc' <= Tests whether one value is less than or equal to <= 'abc' > or > Tests whether one value is greater than > 'abc' >= or >= Tests whether one value is greater than or equal to >= 'abc' and Combines two expressions, returning a value of true only if both expression are = 'abc' = 'xyz' or Combines two expressions, returning a value of true if either expression is = 'abc' = 'xyz' notNegates the value of the expression, changing true to false or false to true >= 'abc') NOTE: Because XML treats the left angle bracket character ( ).

XSLT - test XSLT Conditional Name Phone ( )

Sample XSLT - Test 03_xslt_test.xml 03_xslt_test.xsl

Adding Conditional Choices The xsl:if instruction only allows for one condition and one resulting action. You can use xsl:choose when you want to test for several different conditions, and react accordingly to each one.

To add conditional choices 1)Within a template rule, type. 2)Type <xsl:when to begin the condition. 3)Then, type test="expression", where expression specifies node set, a string, or a number. 4)Next, type > to close the tag. 5)Specify the processing that should take place if the node set, string, or number tested in Step 3 is not empty (or equal to zero, in the case of numbers). 6)Type to complete the condition. 7)Repeat Steps 2-6 for each condition. 8)If desired, type. Specify what should happen if none of the conditions specified by the xsl:when elements are true. Then, type, 9)Finally, type to complete the instruction. NOTE: In the case of multiple conditions, once a condition is found to be true, all the remaining conditions are ignored (even if there is another true condition). The action contained in this first true condition is the only one performed.

XML – to add conditional choices NAME 1 NAME 3

Sample XSLT Choose 04_xslt_choose.xml 04_xslt_choose.xsl

Sorting Nodes before Processing By default, nodes are processed in the order in which they appear in the XML source document. If you would like to process them in some other order, you can add an xsl:sort element when you use xsl:for-each.

To sort nodes before processing 1)Directly after an xsl:for-each element type <xsl:sort. 2)Then, type select="criteria", where criteria is an expression that specifies the node (key) on which the source nodes should be sorted. 3)If desired, type order="descending". The default is for nodes to be sorted in ascending order. 4)If desired, then type data-type="text" or data-type="number" depending on what you are sorting. The default is text. 5)Finally, type /> to close the instruction. 6)Repeat Step 1 – 5 to define as many sorting parameters as desired. NOTE: Be sure to specify the correct data-type in Step 4

XSLT - sort

Sample XSLT - Sort 05_xslt_sort.xml 05_xslt_sort.xsl

Generating Output Attributes When you are transforming your XML source document to an HTML, it is often useful to be able to add attributes and values to a given output element.

To generate output attributes 1)Directly after the opening tag of the element in which this new attribute should appear, type <xsl:attribute. 2)Then, type name="att_name", where att_name is the name that the attribure should have in the element. 3)Next, type > to close the tag. 4)Specify the value of the new attribute using XSLT instructions or literals. 5)Finally, type to complete the attribute generation. 6)Repeat Steps 1-5 to define as many attributes as desired.

XSL – generating output attribute background-color : yellow; font-size : 30 ( )

Sample XSLT – output attributes 06_xslt_attribute.xml 06_xslt_attribute.xsl

Create and Apply Templates The root template is the first thing processed in an XSLT style sheet. This template is the set of rules applied to the root node of the XML source document. XSLT allows you to create more templates than just the root template. This allows you to create different sets of processing rules to apply to different parts of your XML.

To Create a Template 1)Type <xsl:template to begin the template. 2)Then, type match="pattern", where pattern identifies the node(s) of the XML document to which the template will be applied. 3)Next, type > to close the tag. 4)Specify all the transformations that should happen when a node is found that matches the pattern in Step 2. 5)Finally, type to complete the template. NOTE: The root template is simply a template with a pattern that matches the root node. Only the root template is called automatically. All other templates must be applied manually. Otherwise, they are simply ignored.

To Apply a Template 1)Within any template, type <xsl:apply-templates. 2)Then, type select="expression", where expression identifies the node(s) of the XML document whose templates should be applied. 3)Finally, type /> to complete the instruction. NOTE: If you have multiple templates in your style sheet, the order of the xsl:apply-templates elements determines the order in which the templates are processed. If you don’t specify the select attribute in Step 2 above, the processor will look for and apply a template to each of the current node’s children.

XSL – to apply a template ………… …………. ( )

Sample XSLT - template 07_xslt_template.xml 07_xslt_template.xsl