Displaying XML Data with XSLT

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

XSLT 11-Apr-17.
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.
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]
1 CP3024 Lecture 9 XML revisited, XSL, XSLT, XPath, XSL Formatting Objects.
Lecture 10. Back to the table example We can now return to considering in detail the first XSLT style-sheet we saw –The one which, when applied to the.
Rendering XML documents with XSL The most powerful approaches to rendering XML documents involve using XSL (eXtensible Stylesheet Language) XSL enables.
XSL Unit 6 November 2. XSL –eXtensible Stylesheet Language –Basically a stylesheet for XML documents XSL has three parts: –XSLT –XPath –XSL-FO.
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
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)
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 Fundementals XML vs.. HTML XML vs.. HTML XML Document (elements vs. attributes) XML Document (elements vs. attributes) XML and RDBMS XML and RDBMS.
JSP Standard Tag Library
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
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,
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 34 - Case Study: Active Server Pages and XML Outline 34.1 Introduction 34.2 Setup and Message.
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 "
Extensible Stylesheet Language Chao-Hsien Chu, Ph.D. School of Information Sciences and Technology The Pennsylvania State University XSL-FO XSLT.
XML Part 2 Josh Steele CS 6704, Spring XML Part 2 XSL/XSLT –Structure Revisited –Definition –Example –Components XML’s Linking Languages XML’s Uses.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Lecture 11 XSL Transformations (part 1: Introduction)
HTML Basics BCIS 3680 Enterprise Programming. Web Client/Server Architecture 2  Your browser (the client) requests a Web page from a remote computer.
1 Introduction  Extensible Markup Language (XML) –Uses tags to describe the structure of a document –Simplifies the process of sharing information –Extensible.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Applying eXtensible Style Sheets (XSL) Ellen Pearlman Eileen Mullin Programming.
ITB Web programming for E- Commerce 1 ITB6227 Programming for E-COMMERCE Lecture Presentation of XML Documents.
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.
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.
 XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents.
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.
C Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Introduction to XML Standards.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
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.
1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)
1 XSLT XSLT (extensible stylesheet language – transforms ) is another language to process XML documents. Originally intended as a presentation language:
Session IV Chapter 14 – Chapter 14 – XSLThttp://
XSLT: The XML transformation language
Product Training Program
XML for .NET Session 1 Introduction to XML Introduction to XSLT
XML Introduction Bill Jerome.
ASP.NET Forms.
Unit 4 Representing Web Data: XML
Reference: cit /Lectures/xslt.ppt
Using XSLT and XPath to Transform XML Documents
XSLT 1.0.
Extensible Markup Language XML
ACG 4401 XSLT Extensible Stylesheet Language for Transformations
Extensible Markup Language
Applying eXtensible Style Sheets (XSL)
XML WITH CSS.
WHAT IS XSLT?.
ASP.NET.
XSL XSLT and XPath 24-Feb-19.
XML: eXtensible Markup Language
Attributes, Empty-Element Tags, and XSL
XSL XSLT and XPath.
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:

Displaying XML Data with XSLT Session 1 Introduction to XML Introduction to XSLT Programmatically Reading XML Documents Introduction to XPATH

What is XSL? XSL, or the eXtensible Stylesheet Language, is a language used for transforming XML data from one format to another. Realize that XSLT transforms an initial XML document to a different XML document! That is, after the transformation we are still left with an XML document, not text. For example, using XSL we can translate XML into XHTML, and thereby display XML data in a Web page. (XHTML is HTML formatted according to XML rules.)

What is XSL? XSL consists of three parts: XSLT – (XSL Tranformation) the syntax used for transforming an XML document to another format. XPath – the syntax used to access particular elements or attributes of an XML document. XSL Formatting Objects – a syntax for formatting XML documents based on their data and for formatting data differently based on the device viewing the data. We will only be discussion XSLT and XPath. For more information on XSL Formatting Objects, see: http://www.w3.org/TR/xsl/slice6.html

What is XSL? In this presentation, we’ll examine how to use XSLT to transform an XML document into HTML. However, XSLT can be used to transform XML format to any other text format. We’ll examine the .NET Framework’s XslTransform class, as well as the ASP.NET Xml Web control. Before we begin, though, we need to examine XPath.

XSLT – XSL Transform XSLT is a syntax for transforming XML data into an alternate format. The XSLT syntax can appear in an XML-formatted file (typically with a .xsl extension). The XSLT commands are designed to allow for iterating through a list of XML elements, and retrieving the text and/or attribute values of specific XML elements.

The Basic Structure of XSLT The root element for an XSLT file is the <xsl:stylesheet> element. (Note the xsl namespace.) <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> … XSLT syntax goes here … </xsl:stylesheet>

A Quick Refresher on Namespaces XSLT uses the xsl namespace, which is specified via: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> Note that the xsl namespace prefix is used. It’s namespace value must be the specific URL you see above…

The Components of XSLT Recall that XSLT is expressed as an XML-formatted document. There are a number of common XSLT elements that specify how the XML data should be transformed: <xsl:template> <xsl:for-each> <xsl:value-of>

Examining <xsl:template> An XSLT document can contain an arbitrary number of <xsl:template> elements. Each <xsl:template> element must include a match attribute, whose value is an XPath expression that indicates what XML nodes the <xsl:template> tag will work with.

Examining <xsl:template> For example, if we wanted to list each file on the filesystem, we’d need to first add an <xsl:template> element that matches with the <file> elements. <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/filesystem//file"> … </xsl:template> </xsl:stylesheet>

Example of <xsl:template> <xsl:template match="/filesystem//file">

Examining <xsl:value-of> The <xsl:value-of> element accesses the text or attribute value of an element. The <xsl:value-of> element requires a select attribute, which specifes the name of the element whose text node to retrieve, or the attribute name.

Example using <xsl:value-of> The following XSLT document would output the names of all of the folders in the file system. <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/filesystem/drive//folder"> <xsl:value-of select="@name" /> </xsl:template> </xsl:stylesheet>

Examining <xsl:for-each> The <xsl:for-each> element iterates through all of the children elements of a specified element. The <xsl:for-each> element has a select attribute (like <xsl:value-of>), which specifies which element’s children to iterate through. <xsl:template match="/filesystem/drive"> <xsl:value-of select="@letter" /> <xsl:for-each select="folder"> <xsl:value-of select="@name" /> </xsl:for-each> </xsl:template> What would this XSLT document’s output be?

Creating XSLT Files in VS.NET Go to File/New/File and select the XSLT File option.

Creating XSLT Files in VS.NET By default, VS.NET creates the following XSLT content for a new XSLT file: <?xml version="1.0" encoding="UTF-8" ?> <stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform"> </stylesheet>

Creating XSLT Files in VS.NET However, to transform XML files with XSLT the XSLT file must contain the xsl namespace, like so (the added parts are in bold): <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> </xsl:stylesheet>

Fixing this Problem There are two ways to “fix” this problem: Edit the XSLT file by hand each time you create a new one, adding the xsl namespace where needed. Edit the files: <VS.NET Dir>\Vb7\VBWizards\XSLTFile\Templates\1033\XSLTFile.xslt - and - <VS.NET Dir>\VC#\CSharpProjectItems\XSLTFile.xslt

Converting XML to HTML Using XSLT XSLT is commonly used to transform XML data to HTML. For example, we might want to display the file system information on a Web page in the following format: Drive Name All Folder Names in Drive File Name(s) for Specific Drive

Building the XSLT File Piecemeal What XSLT syntax would we want to enumerate the <drive> elements? <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/filesystem/drive"> </xsl:template> </xsl:stylesheet> We could have done an <xsl:template match=“/”> and then an <xsl:for-each select=“filesystem/drive”>

Building the XSLT File Piecemeal Now, how would we display the <drive> element’s letter attribute in a bold font? <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/filesystem/drive"> <b><xsl:value-of select="@letter" /></b> </xsl:template> </xsl:stylesheet>

Building the XSLT File Piecemeal Now we need to iterate through all of the folders for the <drive> element using an unordered list. <xsl:stylesheet …> <xsl:template match="/filesystem/drive"> <b><xsl:value-of select="@letter" /></b> <ul> <xsl:for-each select=“//folder"> </xsl:for-each> </ul> </xsl:template> </xsl:stylesheet> Why //folder and not just folder?

Building the XSLT File Piecemeal Now we need to show each <folder> element’s name attribute and then enumerate through its <file> elements. <xsl:template match="/filesystem/drive"> <b><xsl:value-of select="@letter" /></b> <ul> <xsl:for-each select=“//folder"> <li><xsl:value-of select="@name" /></li> <ul> <xsl:for-each select=“file"> </xsl:for-each></ul> </xsl:for-each> </ul> </xsl:template>

Building the XSLT File Piecemeal Finally, we need to display each <file> attribute’s text content (its filename). <xsl:for-each select=“//folder"> <li><xsl:value-of select="@name" /></li> <ul> <xsl:for-each select=“file"> <li><xsl:value-of select=“text()" /></li> </xsl:for-each> </ul> </xsl:for-each>

Creating the XSLT File in VS.NET Let’s create the XSLT file in VS.NET – please follow along. Now that we have an XSLT file, let’s see how to apply it to an XML document programmatically!

Using the XslTransform Class The XslTransform class is found in the System.Xml.Xsl namespace. Like with the XmlDocument class, the first thing we need to do with an XslTransform class is Load XSLT into it via the Load() method. For more information on the XslTransform class, start with pg. 315 in the XML and ASP.NET book.

The XslTransform Class’s Load Method The Load() method can accept: A string that points to a URL or file path, or An XmlReader class instance, or A class instance that implements the IXPathNavigable interface (typically an XmlNode, XmlDocument, or XmlPathDocument). Refer to pg. 318, Table 6.12

Transforming an XML Document To transform an XML document with the XslTransform class, use the Transform() method. There are a number of variants of this method. For example, you can specify the input XML file and the output XML file, or the XmlDocument to transform and a Stream to write the output. Examine the docs…

Creating an Application to Perform Transformations We will create a new VS.NET Windows Application (either in VB.NET or C#) (feel free to follow along). Our application will allow the user to, upon clicking a button, specify an XML file, an XSL file, and an output file, and the XML document will be transformed using the XSLT, with the resulting XML saved to the specified output file path.

Performing the Transformation The source code for actually performing the transformation is trivial: XslTransform xslt = new XslTransform(); xslt.Load(xslFile); xslt.Transform(xmlFile, outputFile); The above assumes xslFile is a file path for the XSL file, xmlFile a file path for the XML document and outputFile a file path for the transformed output to be written.

Performing the Transformation (in VB.NET) Dim xslt as XslTransform = New XslTransform() xslt.Load(xslFile) xslt.Transform(xmlFile, outputFile)

When Using XslTransform Be sure to add the appropriate Import or using statements. Imports System.Xml Imports System.Xml.Xsl - and for C# - using System.Xml; using System.Xml.Xsl;

Some Notes About the Transformation Examine the transformed content: <?xml version="1.0" encoding="utf-8"?><b>C</b><br /><ul><li>My Programs</li><ul></ul><li>Games</li><ul></ul><li>Quake</li><ul><li>quake.exe</li><li>README.txt</li><li>EULA.txt</li></ul><li>SavedGames</li><ul><li>game1.sav</li><li>game2.sav</li></ul><li>Windows</li><ul><li>win.exe</li></ul><li>System32</li><ul></ul></ul><b>D</b><br /><ul><li>Backup</li><ul><li>2003-06-01.bak</li><li>2003-06-07.bak</li></ul></ul>

Some Notes About the Transform Notice that the transform content contains the XML pre-processing directive: All of the content is jammed together – white space was not preserved. (This doesn’t really matter if you’re translating XML to be displayed in an HTML Web page, since HTML is not whitespace-sensitive.) <?xml version="1.0" encoding="UTF-8" ?>

“Fixing” the Output To remove the pre-processing directive, we can use the XSLT <xsl:output method="html" /> element. To preserve whitespace, we can use <xsl:preserve-space elements=“elementList” /> Note that both of these elements must appear at the same nesting level of the <xsl:template> element.

The New, “Fixed” XSLT Document <?xml version="1.0" encoding="utf-8" ?> <xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <xsl:preserve-space elements="*" /> <xsl:template match="/filesystem/drive"> <b><xsl:value-of select="@letter" /></b><br /> <ul> <xsl:for-each select=".//folder"> <li><xsl:value-of select="@name" /></li> <xsl:for-each select="file"> <li><xsl:value-of select="text()" /></li> </xsl:for-each> </ul> </xsl:template> </xsl:stylesheet>

Displaying Formatted XML Content in a Web Page Displaying XSLT-formatted XML content in a Web page can be done in one of two ways: Client-side – This technique sends the raw XML and XSLT content to the Web browser, which then formats the XML. (User must use a Web browser that supports this features (IE 5.0+, for example).) Server-side – The XML is transformed on the Web server, and the transformed content is then sent to the Web browser – works with any Web browser.

Server-Side Transformation with ASP.NET With ASP.NET, performing server-isde transformations is a breeze with the XML Web control. With the XML Web control, simply specify the XML file path and the XSL file path, and it does everything for you!

Server-Side Transformation with ASP.NET For example, the following ASP.NET Web page would display the HTML in the filesystem transformation we examined just a few slides ago: <html> <body> <asp:xml runat="server“ DocumentSource="FileSystem.xml" TransformSource="FormatFileSystemData.xsl" /> </body> </html>

The Visitor’s Web Browser: The HTML Sent to the Browser: The Visitor’s Web Browser:

For More Information on the ASP.NET XML Web Control… See pgs. 322-324 in XML and ASP.NET See http://aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=204 See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconXMLWebServerControl.asp

Conclusion XSL is designed to translate XML content from one form to another. XSL is comprised of: XPath XSLT XSL Formatting Objects (XSLFO or XFO) XSLT transformations are specified via an XML-formatted document that includes templates, for-each, and value-of constructs.

Questions?