New Perspectives on XML

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.
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
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.
XSL Transformations (XSLT) Meghasyam Bokam April’1 st, 2002.
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
SD2520 Databases using XML and JQuery
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.
IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
CIS 451: XSL Dr. Ralph Westfall February, Problems With XML no formatting capabilities contra formatting tags like, etc. in HTML CSS can be used.
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,
XML About XML Things to be known Related Technologies XML DOC Structure Exploring XML.
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 Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
XML 2nd EDITION Tutorial 1 Creating An Xml Document.
 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 – XSL: Extensible Stylesheet Language Transformations (XSLT) Outline 12.1Introduction 12.2Setup.
XP 1 Creating an XML Document Developing an XML Document for the Jazz Warehouse XML Tutorial.
XP New Perspectives on XML, 2nd Edition Tutorial 2 1 TUTORIAL 2 WORKING WITH NAMESPACES.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Applying eXtensible Style Sheets (XSL) Ellen Pearlman Eileen Mullin Programming.
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.
1 Tutorial 12 Working with Namespaces Combining XML Vocabularies in a Compound Document.
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.
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.
University of Nottingham School of Computer Science & Information Technology Introduction to XML 2. XSLT Tim Brailsford.
XSLT: How Do We Use It? Nancy Hallberg Nikki Massaro Kauffman.
1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)
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.
In this session, you will learn to:
XSLT: The XML transformation language
Working with Cascading Style Sheets
CSS Cascading Style Sheets
XSL Transformations (XSLT)
In this session, you will learn to:
XML Introduction Bill Jerome.
Unit 4 Representing Web Data: XML
4.01 Cascading Style Sheets
Displaying XML Data with XSLT
Madam Hazwani binti Rahmat
Reference: cit /Lectures/xslt.ppt
Using XSLT and XPath to Transform XML Documents
Using Extension Elements and Extension Functions with XSLT and XPath
eXtensible Stylesheet Language
XSLT 1.0.
ACG 4401 XSLT Extensible Stylesheet Language for Transformations
IS333: MULTI-TIER APPLICATION DEVELOPMENT
Session II Chapter 2 - XSLT
Chapter 7 Representing Web Data: XML
Creating an XML Document
Second CSS Lecture Applications to XML
Applying eXtensible Style Sheets (XSL)
XML WITH CSS.
CS 431 – February 28, 2005 Carl Lagoze – Cornell University
WHAT IS XSLT?.
Tutorial 3 Working with Cascading Style Sheets
Web Design & Development
Attributes, Empty-Element Tags, and XSL
4.01 Cascading Style Sheets
More XML XML schema, XPATH, XSLT
7 September 1999 The Basics of XSLT Assuming a basic knowledge of XML and XML Namespaces Thomas G. Habing Grainger Engineering Library Information Center.
Presentation transcript:

New Perspectives on XML TUTORIAL 6 WORKING WITH XSLT New Perspectives on XML Tutorial 6

New Perspectives on XML OBJECTIVES In this chapter, you will: Learn about the history and theory of XSL Create an XSLT style sheet Be introduced to syntax of the XPath language Transform an XML document into an HTML file Create templates to format sections of the XML document New Perspectives on XML Tutorial 6

New Perspectives on XML OBJECTIVES In this chapter, you will: Sort the contents of an XML document Create conditional nodes to generate different HTML code Use predicates to select subsets of an XML document Insert new elements and attributes in the transformed document New Perspectives on XML Tutorial 6

New Perspectives on XML The Limitation of CSS CSS has several limitations, including: CSS can apply a style to element, but can’t change the format of the content itself (e.g., can’t change date from June 28, 2004 to 6/28/2004) CSS doesn’t provide easy methods to display images or insert hypertext links CSS displays only element content, not element attributes It is difficult to display content from several documents An element can be formatted only one way in a document New Perspectives on XML Tutorial 6

New Perspectives on XML WORKING WITH XSL In 1998, the W3C developed the Extensible Style sheet Language, or XSL XSL is composed of three parts: XSL-FO (Extensible Style sheet Language – Formatting Objects) XSLT (Extensible Style sheet Language Transformations) XPath New Perspectives on XML Tutorial 6

INTRODUCING XSL-FO, XSLT, AND XPATH XSL-FO is used to implement page layout and design XSLT is used to transform XML content into another presentation format XPath is used to locate information from an XML document and perform operations and calculations upon that content New Perspectives on XML Tutorial 6

INTRODUCING XSLT STYLE SHEETS AND PROCESSORS An XSLT style sheet contains instructions for transforming the contents of an XML document into another format An XSLT style sheet document is itself an XML document An XSLT style sheet document has an extension .xsl New Perspectives on XML Tutorial 6

GENERATING A RESULT DOCUMENT An XSLT style sheet converts a source document of XML content into a result document by using the XSLT processor New Perspectives on XML Tutorial 6

INTRODUCING XSLT STYLE SHEETS AND PROCESSORS The transformation can be performed by a server or a client In a server-side transformation, the server receives a request from a client, applies the style sheet to the source document, and returns the result document to the client In a client-side transformation, a client requests retrieval of both the source document and the style sheet from the server, then performs the transformation, and generates the result document New Perspectives on XML Tutorial 6

CREATING AN XSLT STYLE SHEET To create an XSLT style sheet, the general structure: <?xml version =“1.0”> <xsl:stylesheet version = 1.0 xmlns:xsl =“http://www.w3.org/1999/XSL/Transform”> Content of the style sheet </xsl:stylesheet> To link the XML file to the style sheet, the syntax is: <?xml-stylesheet type=“text/xsl” href=“URI” ?> <XSL:tranform...> New Perspectives on XML Tutorial 6

WORKING WITH DOCUMENT NODES Under XPath, each component in the document is referred to as a node, and the entire structure of the document is a node tree The node tree consists of the following objects: the XML document itself Comments processing instructions Namespaces elements, element text element attributes New Perspectives on XML Tutorial 6

New Perspectives on XML NODE TREE EXAMPLE New Perspectives on XML Tutorial 6

WORKING WITH DOCUMENT NODES At the top of the node is the root node A node that contains other nodes is called a parent node, and the nodes contained in the parent are called child nodes Nodes that share a common parent are called sibling nodes Any node below another node is referred to as a descendant of that node The node at the top of the branch is referred to as the ancestor of all nodes that lie beneath it New Perspectives on XML Tutorial 6

WORKING WITH DOCUMENT NODES Nodes are distinguished based on the object they refer to in the document A node for an element is called an element node The node that stores element attributes is called an attribute node New Perspectives on XML Tutorial 6

USING XPATH TO REFERENCE A NODE XPath provides the syntax to refer to the various nodes in the node tree The syntax is used by operation system to specify file pathnames The location of a node can be expressed in either absolute or relative terms XPath also does data extraction New Perspectives on XML Tutorial 6

USING XPATH TO REFERENCE A NODE For absolute path, XPath begins with the root node, identified by a forward slash and proceeds down the levels of the node tree An absolute path: /child1/child2/child3/… Ex., /portfolio/stock/description To reference an element without regard to its location in the node tree, use a double forward slash with the name of the descendant node A relative path : //descendant (e.g.,//yield ) New Perspectives on XML Tutorial 6

New Perspectives on XML RELATIVE PATHS With a relative path, the location of the node is indicated relative to a specific node in the tree called the context node New Perspectives on XML Tutorial 6

New Perspectives on XML RELATIVE PATHS <portfolio> <author>Kevin Summers</author> <date>11/14/2004</date> <time>14:54</time> <stock> <name symbol="AA">Aluminum Company of America</name> <description>Alcoa Inc. is a producer…</description> <category>Industrials</category> <link>http://www.alcoa.com</link> ….. <today open="29.60" high="29.94" low="28.59" current="29.20" vol="2.94" /> <five_day> <day open="31.20" high="32.61" low="30.15" close="30.51" vol="6.70" date="11/17/2004">1</day> …. </five_day> </stock> เพิ่ม stock.xml New Perspectives on XML Tutorial 6

New Perspectives on XML five_day day New Perspectives on XML Tutorial 6

New Perspectives on XML RELATIVE PATHS *Assumes that the stock element is the context node New Perspectives on XML Tutorial 6

REFERENCING GROUPS OF ELEMENTS XPath allows you to refer to groups of nodes by using the wildcard character (*) To select all of the nodes in the node tree, you can use the path: //* The (*) symbol matches any node, and the (//)symbol matches any level of the node tree Example: /portfolio/stock/* To combine different paths into a single expression Example: /portfolio/date | /portfolio/time //name | //description New Perspectives on XML Tutorial 6

REFERENCING ATTRIBUTE NODES XPath uses different notation to refer to attribute nodes The syntax for attribute node is: @attribute where attribute is the name of the attribute Example: /portfolio/stock/name/@symbol If the stock element is the context node, this would be: name/@symbol If name is the context node, the relative path is @symbol New Perspectives on XML Tutorial 6

WORKING WITH TEXT NODES The text contained in an element node is treated as a text node The syntax for selecting a text node is: @text() Example: /portfolio/stock/name/text() To match all text nodes in the document, use: //text() New Perspectives on XML Tutorial 6

CREATING THE ROOT TEMPLATE The first task in designing the stock.xsl style sheet is to create a template for the root node A template is a collection of elements that define how a particular section of the source document should be transformed in the result document The root template sets up the initial code for the result document New Perspectives on XML Tutorial 6

CREATING A ROOTTEMPLATE To create a root template, the syntax is: <xsl:template match=“/”> XSLT and Literal Result Elements </xsl:template> where node is either the the name of a node from the source document’s node tree, or an XPath expression that points node in the tree New Perspectives on XML Tutorial 6

New Perspectives on XML CREATING A TEMPLATE To create a root template, the syntax is: <xsl:template match=“node”> XSLT and Literal Result Elements </xsl:template> New Perspectives on XML Tutorial 6

CREATING THE ROOT TEMPLATE A template contains two types of content: XSLT elements and literal result elements XSLT elements are those elements that are part of the XSLT namespace and are used to send commands to the XSLT processor A literal result element is text sent to the result document, but not acted upon by the XSLT processor New Perspectives on XML Tutorial 6

CREATING THE ROOT TEMPLATE EXAMPLE <?xml version='1.0' ?> <xsl:stylesheet version='1.0' xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="4.0" /> <xsl:template match="/"> <html> <head><title>Stock Information</title> <link href="stock.css" rel="stylesheet" type="text/css" /> </head> <body> <h1 class="title">Hardin Financial</h1> <h2 class="title">Stock Information</h2> <xsl:value-of select="portfolio/author" /> </body> </html> </xsl:template> </xsl:stylesheet> XSLT element Listeral result elements XSLT element h1, h2 {font-family: Arial, Helvetica, sans-serif} h1.title, h2.title {color:red} h2.title {font-style:italic; margin-top:-25px} New Perspectives on XML Tutorial 6

Stock.xml (invoking the xslt file) <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="stock.xsl" ?> <portfolio> <author>Kevin Summers</author> <date>11/14/2004</date> <time>14:54</time> <stock> <name symbol="AA">Aluminum Company of America</name> …. </stock> </portfolio> stock.xml เพิ่ม New Perspectives on XML Tutorial 6

SPECIFYING THE OUTPUT METHOD By default, the XSLT processor will render the result document as an XML file To control how the processor formats the source document, you can specify the output method using the <xsl:output/> element New Perspectives on XML Tutorial 6

ATTRIBUTS OF THE <XSL:OUTPUT/> ELEMENT New Perspectives on XML Tutorial 6

New Perspectives on XML To create an HTML 4.0 file, the output tag is <xsl:output method=“html” version=“4.0” /> To create a piece of XML document and remove an opening XML declaration, the output tag is <xsl:output method=“xml” version=“1.0” omit-xmldeclaration=“yes” /> To create a plain text file, the output tag is <xsl:output method=“text” /> New Perspectives on XML Tutorial 6

TRANSFORMING A DOCUMENT A browser with a built-in XSLT processor allows you to view the result document Alternatively, you can use XML Spy to create the result document as a separate file, and then view that file in your browser Most XSLT processors provide the capability to create the result document as a separate file New Perspectives on XML Tutorial 6

VIEWING THE RESULT DOCUMENT IN A BROWSER Internet Explorer 6.0 contains built-in XSLT processor You can view the results of the transformation by opening the result document in the browser New Perspectives on XML Tutorial 6

CREATING AN HTML FILE IN XML SPY One advantage of creating a separate HTML file is that it can be viewed in any Web browser You have to regenerate the HTML file every time you make a change to the source document, or the style sheet The XSLT processor adds one extra line to the document that provides additional information to the browser about the content of the document and its encoding <META http-equiv=“Content-Type” content=“text/html; charset=UTF-16”> New Perspectives on XML Tutorial 6

TRANSFORMING THE SOURCE DOCUMENT IN XML SPY New Perspectives on XML Tutorial 6

New Perspectives on XML INSERTING A NODE VALUE To insert a node’s value into the result document, the syntax is: <xsl:value-of> select=“XPath Expression” /> where XPath Expression is an expression that identifies the node from the source document’s node tree If the node contains child elements in addition to text content, the text in those child nodes appears as well New Perspectives on XML Tutorial 6

New Perspectives on XML INSERTING A NODE VALUE For example, if the element date and time need to be display as follows: Last Updated: 11/24/2004 at 14:54 The names of the stocks are also displayed as h3 headings Add the following code to the root template of stock.xsl New Perspectives on XML Tutorial 6

NODE TREE EXAMPLE (Stock.xml) Root(/) portfolio author date time stock name Five_day day New Perspectives on XML Tutorial 6

INSERTING A NODE VALUE EXAMPLE stock.xsl …. <xsl:template match="/"> <html> … <body> <div id="datetime"><b>Last Updated: </b> <xsl:value-of select="portfolio/date" /> at <xsl:value-of select="portfolio/time" /> </div> <h1 class="title">Hardin Financial</h1> <h2 class="title">Stock Information</h2> <h3 class="name"> <xsl:value-of select="portfolio/stock/name" /> </h3> </body> เพิ่ม =portfolio/date/text() New Perspectives on XML Tutorial 6

INSERTING A NODE VALUE XP EXAMPLE #datetime {font-size:8pt; position:absolute; top:0px; left:480px} h1, h2 {font-family: Arial, Helvetica, sans-serif} h1.title, h2.title {color:red} h2.title {font-style:italic; margin-top:-25px} h3.name {color: brown; clear: right} stock.css Date and time stock name New Perspectives on XML Tutorial 6

PROCESSING A BATCH OF NODES To process a batch of nodes, the syntax is: <xsl:for-each select=“XPath Expression” /> XSLT and Literal Elements </xsl:for-each> where XPath Expression is an expression that defines the group of nodes to which the XSLT and literal result elements are applied New Perspectives on XML Tutorial 6

PROCESSING A BATCH OF NODES EXAMPLE root/ portfolio stock name stock name New Perspectives on XML Tutorial 6

PROCESSING A BATCH OF NODES EXAMPLE Stock name New Perspectives on XML Tutorial 6

WORKING WITH TEMPLATES To apply a template in the result document, use the XSLT element <xsl:apply-templates select=“XPath Expression” /> where XPath Expression indicates the node template to be applied New Perspectives on XML Tutorial 6

CREATING TEMPLATE EXAMPLE “portfolio/stock” <xsl:template match=“stock”> <xsl:value-of select=“name” /> </xsl:template> New Perspectives on XML Tutorial 6

CREATING TEMPLATE EXAMPLE Stock name New Perspectives on XML Tutorial 6

USING THE BUILT-IN TEMPLATES Each node has its own built-in template. The built-in template for element nodes matches the document root and all elements in the node tree The built-in template for text nodes matches all text nodes and causes their values to appear in the result document <xsl:template match=“*|/” > <xsl:apply-templates /> </xsl:template> New Perspectives on XML Tutorial 6

CREATING THE STOCK TEMPLATE EXAMPLE <xsl:template match="/"> <html> … <h2 class="title">Stock Information</h2> <xsl:apply-templates select="portfolio/stock" /> </body> </html> </xsl:template> <xsl:template match="stock"> <div class="stock_info"> <xsl:apply-templates select="name" /> <p><xsl:value-of select="description" /></p> </div> <xsl:template match="name"> <h3 class="name"> <xsl:value-of select="." /> </h3> </xsl:stylesheet> root/ portfolio stock name description stock name name New Perspectives on XML Tutorial 6

CREATING THE STOCK TEMPLATE EXAMPLE apply-templates select="name" apply-templates select="portfolio/stock" (For the first time invocation) apply-templates select="name“ (two names) New Perspectives on XML Tutorial 6

CREATING THE STOCK TEMPLATE EXAMPLE <h2 class="title">Stock Information</h2> <xsl:apply-templates select="portfolio/stock" /> </body> </html> </xsl:template> <xsl:template match="stock"> <h3 class="name"> <xsl:value-of select="name" /> </h3> <div class="stock_info"> <p> <xsl:value-of select="description" /> </p> </div> </xsl:stylesheet> root/ portfolio stock name description stock What is the results? name name New Perspectives on XML Tutorial 6

CREATING THE STOCK TEMPLATE EXAMPLE portfolio <h2 class="title">Stock Information</h2> <xsl:apply-templates select="portfolio/stock" /> </body> </html> </xsl:template> <xsl:template match="stock"> <table class="summary" border="1"> <tr> <th colspan= "2" class="summtitle">Summary Information </th> </tr> <th class="summary">Year High</th> <td class="number"><xsl:value-of select="year_high" /></td> <th class="summary">Year Low</th> <td class="number"><xsl:value-of select="year_low"/></td> stock table.summary {font-size: 10pt; float:right; margin-top:30px} name year_high year_low stock name year_high year_low New Perspectives on XML Tutorial 6

CREATING THE STOCK TEMPLATE EXAMPLE New Perspectives on XML Tutorial 6

Working with Attribute Nodes If the stock name needs to be appended with value of symbol attribute <stock> <name symbol="AA">Aluminum Company of America</name> The result of displaying the stock name is Aluminum Company of America (AA) New Perspectives on XML Tutorial 6

Working with Attribute Nodes The name template can be modified into <xsl:template match="name"> <h3 class="name"> <xsl:value-of select="." /> (<xsl:value-of select="@symbol" />) </h3> </xsl:template> New Perspectives on XML Tutorial 6

New Perspectives on XML SORTING NODES By default, nodes are processed in document order, by their appearance in the document To specify a different order, XSLT provides the <xsl:sort> element This element can be used with either the <xsl:apply-templates> or the <xsl:for-each> element New Perspectives on XML Tutorial 6

New Perspectives on XML SORTING NODES The <xsl:sort> element contains several attributes to control how the XSLT process sorts the nodes in the source document.The syntax is <xsl:sort select=“expression” data-type=“type” order=“type” case-order=“type” /> The select attribute determines the criteria under which the context node is sorted The data-type attribute indicates the type of data The order attribute indicates the direction of the sorting (ascending or descending) New Perspectives on XML Tutorial 6

EXAMPLE OF SORTING NODES <xsl:apply-templates select=“portfolio/stock”> <xsl:sort select=“name” /> </xsl:apply-templates> <xsl:apply-templates select=“day”> <xsl:sort data-type=“number” order=“descending” /> <xsl:sort select=“category” /> <xsl:apply-templates select=“portfolio/stock/name”> <xsl:sort /> </xsl:apply-templates> New Perspectives on XML Tutorial 6

EXAMPLE OF SORTING NODES <xsl:template match="/"> <html> <head><title>Stock Information</title> <link href="stock.css" rel="stylesheet" type="text/css" /> </head> <body> <h1 class="title">Hardin Financial</h1> <h2 class="title">Stock Information</h2> <xsl:apply-templates select="portfolio/stock/name" > <xsl:sort /> </xsl:apply-templates> </body> </html> </xsl:template> <xsl:template match="name"> <h3 class="name"> <xsl:value-of select="." /> </h3> New Perspectives on XML Tutorial 6

EXAMPLE OF SORTING NODES <xsl:template match="/"> <html> <head><title>Stock Information</title> <link href="stock.css" rel="stylesheet" type="text/css" /> </head> <body> <h1 class="title">Hardin Financial</h1> <h2 class="title">Stock Information</h2> <xsl:apply-templates select="portfolio/stock" > <xsl:sort select="name"/> </xsl:apply-templates> </body> </html> </xsl:template> <xsl:template match="stock"> <h3 class="name"> <xsl:value-of select="name" /> </h3> New Perspectives on XML Tutorial 6

CREATING CONDITIONAL NODES XSLT supports two kinds of conditional elements: <xsl:if> <xsl:choose> To apply a format only if a particular condition is met , use the <xsl:if> element To test for multiple conditions and display different outcomes, use the <xsl:choose> element New Perspectives on XML Tutorial 6

CREATING CONDITIONAL NODES The syntax for the <xsl:if> element is: <xsl:if test=“expression”> XSLT and Literal Result Elements </xsl:if> Example: <xsl:if test=“@sysmbol = ‘AA’ ”> <h3><xsl:value-of select=“name” /></h3> New Perspectives on XML Tutorial 6

CREATING CONDITIONAL NODES EXAMPLE The syntax for the <xsl:choose> element is: <xsl:choose> <xsl:when test=“expression1”> XSLT and Literal Result Elements </xsl:when> <xsl:when test=“expression2”> … <xsl:otherwise> </xsl:otherwise> </xsl:choose> New Perspectives on XML Tutorial 6

CREATING CONDITIONAL NODES EXAMPLE Example of using <xsl:choose> element: <xsl:choose> <xsl:when test=“@current < @open”> <img src=“down.gif” /> </xsl:when> <xsl:when test=“@current > @open”> <img src=“up.gif” /> <xsl:otherwise> <img src=“same.gif” /> </xsl:otherwise> </xsl:choose> New Perspectives on XML Tutorial 6

USING COMPARISON OPERATORS AND FUNCTIONS New Perspectives on XML Tutorial 6

CREATING CONDITIONAL NODES EXAMPLE stock <name symbol=…> … <today open=… High=…low=… Current=…vol=…> … New Perspectives on XML Tutorial 6

USING COMPARISON XP OPERATORS AND FUNCTIONS New Perspectives on XML Tutorial 6

WORKING WITH PREDICATES Predicates are XPath expressions that test for a condition and create subsets of nodes that fulfill that condition The predicate can also indicate the position of the node in the node tree The syntax is node [expression] New Perspectives on XML Tutorial 6

WORKING WITH PREDICATES To select the name elements whose symbol attribute is “AA”, the predicate is: name[@symbol = “AA”] To select name elements whose have a symbol attribute => name[@symbol] To select the first stock element => stock[1] To select the last stock element => stock[last()] To select the second stock element =>stock[position() = 2] To combine predicate with XPath expression <xsl:value-of select=“stock[position()=2]/name” /> New Perspectives on XML Tutorial 6

ADDING PREDICATES TO THE ROOT TEMPLATE EXAMPLE portfolio date time stock name category link … stock New Perspectives on XML Tutorial 6

ADDING PREDICATES TO THE ROOT TEMPLATE EXAMPLE Category=“Industrials” New Perspectives on XML Tutorial 6

CREATING ELEMENTS AND ATTRIBUTES To produce the result document as follows: <a href=“http://www.alcoa.com”> <h3 class=“name”> Aluminum Company of America </h3> </a> The problem will occur when the template is created as: <xsl:template match=“name”> <a href=“ <xsl:value-of select=“../link” /> ”> <xsl:value-of select=“.” /> </xsl:template> New Perspectives on XML Tutorial 6

CREATING ELEMENTS AND ATTRIBUTES One way of solving problem is to create the <a> element in the result document. To create an element, XSLT uses the <xsl:element> tag as the following syntax: <xsl:element name=“name” namespace=“URI” use-attribute-sets=“namelist”> XSLT and Literal Result Elements </xsl:element> New Perspectives on XML Tutorial 6

New Perspectives on XML CREATING AN ELEMENT To create the <a> element in the result document, use the <xsl:element> tag The result is => <a> <h3 class=“name”> Aluminum Company of America (AA) </h3> </a> New Perspectives on XML Tutorial 6

New Perspectives on XML CREATING AN ATTRIBUTE Attributes are created in XSLT by using the <xsl:attribute> element The name attribute specifies the name of the attribute The namespace attribute indicates the namespace You can create inline images in the result document by using the attribute tag New Perspectives on XML Tutorial 6

New Perspectives on XML CREATING AN ATTRIBUTE To add the href attribute to the <a> tag, use the <xsl:attribute> element New Perspectives on XML Tutorial 6

New Perspectives on XML CREATING AN ATTRIBUTE The result is : <a href=“http://www.alcoa.com”> <h3 class=“name”> Aluminum Company of America (AA) </h3> </a> New Perspectives on XML Tutorial 6

CREATING COMMENTS AND PROCESSING INSTRUCTIONS The <xsl:comment> element creates the comment You can create a processing instruction by using the <xsl:processing-instruction> element If you want to add a processing instruction to attach the result document to the style.css sheet, use the following code: <?xml-stylesheet href=“styles.css” type=“text.css” ?> New Perspectives on XML Tutorial 6

New Perspectives on XML SUMMARY Extensible Style sheet Language,or XSL, is composed of three parts: XSL-FO, XSLT, and XPath XPath language is used to reference a node Templates are used to format sections of the XML document and transform XML data into a variety of formats New Perspectives on XML Tutorial 6

New Perspectives on XML SUMMARY Nodes can be sorted in either alphabetical or numerical order Comparison elements allow changing the contents of the result document based on the values of the nodes in the source document Predicates are used to create subsets of the source document’s node tree You can insert new elements and attributes in the transformed document New Perspectives on XML Tutorial 6