XSLT Introduction.

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 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.
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.
XSLT 11-Apr-17.
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
SPECIAL TOPIC XML. Introducing XML XML (eXtensible Markup Language) ◦A language used to create structured documents XML vs HTML ◦XML is designed to transport.
XSLT (eXtensible Stylesheet Language Transformation) 1.
XML 6.2 XSL / XSLT 6. What is XSL? XSL stands for eXtensible Stylesheet Language CSS was designed for styling HTML pages, and can be used to style XML.
XSL XSLT and XPath 11-Apr-17.
1 CP3024 Lecture 9 XML revisited, XSL, XSLT, XPath, XSL Formatting Objects.
eXtensible Stylesheet Language
XSL Transformations (XSLT) Meghasyam Bokam April’1 st, 2002.
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)
MC 365 – Software Engineering Presented by: John Ristuccia Shawn Posts Ndi Sampson XSLT Introduction BCi.
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.
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
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
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:
CSE3201/CSE4500 Information Retrieval Systems XSLT – Part 2.
 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.
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 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)
 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.
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 cs XSL XSL is a standard that consists of three parts: XPath (navigation in documents) XPath was taught in the DB course, so it will not be.
XSLT. XSLT stands for Extensible Stylesheet Language Transformations XSLT is used to transform XML documents into other kinds of documents. XSLT can produce.
 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.
CSE3201/CSE4500 Information Retrieval Systems XSLT – Part 2.
XSLT: How Do We Use It? Nancy Hallberg Nikki Massaro Kauffman.
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.
1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)
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.
Session IV Chapter 14 – Chapter 14 – XSLThttp://
XSLT: The XML transformation language
Session II Chapter 14 – XSLT
XML BASICS.
UNIT-V XML Introduction, Structure of XML Document, Document Type Definition, XML Namespaces, XML Schema, Working with DOM and SAX Parser, Working with.
Unit 4 Representing Web Data: XML
Displaying XML Data with XSLT
Reference: cit /Lectures/xslt.ppt
XSLT 1.0.
ACG 4401 XSLT Extensible Stylesheet Language for Transformations
Session II Chapter 2 - XSLT
Applying eXtensible Style Sheets (XSL)
XML WITH CSS.
CS 431 – February 28, 2005 Carl Lagoze – Cornell University
WHAT IS XSLT?.
XSL XSLT and XPath 24-Feb-19.
XML: Introduction to XML
XSL XSLT and XPath.
New Perspectives on XML
eXtensible Markup Language XML
Presentation transcript:

XSLT Introduction

XSLT XSL (eXtensible Stylesheet Language) is a styling language for XML. XSLT stands for XSL Transformations. This tutorial will teach you how to use XSLT to transform XML documents into other formats (like transforming XML into HTML).

<. xml version="1. 0". > <xsl:stylesheet version="1 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">   <html>   <body>     <h2>My CD Collection</h2>     <table border="1">       <tr bgcolor="#9acd32">         <th>Title</th>         <th>Artist</th>       </tr>       <xsl:for-each select="catalog/cd">         <tr>           <td><xsl:value-of select="title"/></td>           <td><xsl:value-of select="artist"/></td>         </tr>       </xsl:for-each>     </table>   </body>   </html> </xsl:template> </xsl:stylesheet> XSLT Example

XSLT Elements Reference Description apply-imports Applies a template rule from an imported style sheet apply-templates Applies a template rule to the current element or to the current element's child nodes attribute Adds an attribute attribute-set Defines a named set of attributes call-template Calls a named template choose Used in conjunction with <when> and <otherwise> to express multiple conditional tests comment Creates a comment node in the result tree

Element Description copy Creates a copy of the current node (without child nodes and attributes) copy-of Creates a copy of the current node (with child nodes and attributes) decimal-format Defines the characters and symbols to be used when converting numbers into strings, with the format-number() function element Creates an element node in the output document fallback Specifies an alternate code to run if the processor does not support an XSLT element for-each Loops through each node in a specified node set if Contains a template that will be applied only if a specified condition is true import Imports the contents of one style sheet into another. Note: An imported style sheet has lower precedence than the importing style sheet include Includes the contents of one style sheet into another. Note: An included style sheet has the same precedence as the including style sheet

Element Description key Declares a named key that can be used in the style sheet with the key() function message Writes a message to the output (used to report errors) namespace-alias Replaces a namespace in the style sheet to a different namespace in the output number Determines the integer position of the current node and formats a number otherwise Specifies a default action for the <choose> element output Defines the format of the output document param Declares a local or global parameter preserve-space Defines the elements for which white space should be preserved processing-instruction Writes a processing instruction to the output sort Sorts the output strip-space Defines the elements for which white space should be removed

Element Description stylesheet Defines the root element of a style sheet template Rules to apply when a specified node is matched text Writes literal text to the output transform value-of Extracts the value of a selected node variable Declares a local or global variable when Specifies an action for the <choose> element with-param Defines the value of a parameter to be passed into a template

XSL(T) Languages XSLT is a language for transforming XML documents. XPath is a language for navigating in XML documents. XQuery is a language for querying XML documents. What is XSLT? XSLT stands for XSL Transformations XSLT is the most important part of XSL XSLT transforms an XML document into another XML document XSLT uses XPath to navigate in XML documents XSLT is a W3C Recommendation

Create an XSL Style Sheet <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>     </tr>     <xsl:for-each select="catalog/cd">     <tr>       <td><xsl:value-of select="title"/></td>       <td><xsl:value-of select="artist"/></td>     </tr>     </xsl:for-each>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet> Create an XSL Style Sheet "cdcatalog.xsl"

<. xml version="1. 0" encoding="UTF-8" <?xml version="1.0" encoding="UTF-8"?> <catalog>   <cd>     <title>Empire Burlesque</title>     <artist>Bob Dylan</artist>     <country>USA</country>     <company>Columbia</company>     <price>10.90</price>     <year>1985</year>   </cd> . . </catalog> Link the XSL Style Sheet to the XML Document "cdcatalog.xml" "cdcatalog.xsl"

RESULT

XSLT <xsl:template> Element An XSL style sheet consists of one or more set of rules that are called templates. A template contains rules to apply when a specified node is matched. The <xsl:template> Element The <xsl:template> element is used to build templates. The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).

Create an XSL Style Sheet <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>     </tr>     <tr>       <td>.</td>       <td>.</td>     </tr>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet> Create an XSL Style Sheet "cdcatalog.xsl"

<. xml version="1. 0" encoding="UTF-8" <?xml version="1.0" encoding="UTF-8"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> <cd> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <country>USA</country> <company>RCA</company> <price>9.90</price> <year>1982</year> </cd> <title>Still got the blues</title> <artist>Gary Moore</artist> <country>UK</country> <company>Virgin records</company> <price>10.20</price> <year>1990</year> <title>Eros</title> <artist>Eros Ramazzotti</artist> <country>EU</country> <company>BMG</company> <year>1997</year>

<cd> <title>One night only</title> <artist>Bee Gees</artist> <country>UK</country> <company>Polydor</company> <price>10.90</price> <year>1998</year> </cd> <title>Sylvias Mother</title> <artist>Dr.Hook</artist> <company>CBS</company> <price>8.10</price> <year>1973</year> <cd> <title>Maggie May</title> <artist>Rod Stewart</artist> <country>UK</country> <company>Pickwick</company> <price>8.50</price> <year>1990</year> </cd> <title>Romanza</title> <artist>Andrea Bocelli</artist> <country>EU</country> <company>Polydor</company> <price>10.80</price> <year>1996</year> <title>When a man loves a woman</title> <artist>Percy Sledge</artist> <country>USA</country> <company>Atlantic</company> <price>8.70</price> <year>1987</year>

<cd> <title>Black angel</title> <artist>Savage Rose</artist> <country>EU</country> <company>Mega</company> <price>10.90</price> <year>1995</year> </cd> <title>1999 Grammy Nominees</title> <artist>Many</artist> <country>USA</country> <company>Grammy</company> <price>10.20</price> <year>1999</year> <title>For the good times</title> <artist>Kenny Rogers</artist> <country>UK</country> <company>Mucik Master</company> <price>8.70</price> <cd> <title>Big Willie style</title> <artist>Will Smith</artist> <country>USA</country> <company>Columbia</company> <price>9.90</price> <year>1997</year> </cd> <title>Tupelo Honey</title> <artist>Van Morrison</artist> <country>UK</country> <company>Polydor</company> <price>8.20</price> <year>1971</year>

<title>Tupelo Honey</title> <cd> <title>Soulsville</title> <artist>Jorn Hoel</artist> <country>Norway</country> <company>WEA</company> <price>7.90</price> <year>1996</year> </cd> <title>The very best of</title> <artist>Cat Stevens</artist> <country>UK</country> <company>Island</company> <price>8.90</price> <year>1990</year> <cd> <title>Big Willie style</title> <artist>Will Smith</artist> <country>USA</country> <company>Columbia</company> <price>9.90</price> <year>1997</year> </cd> <title>Tupelo Honey</title> <artist>Van Morrison</artist> <country>UK</country> <company>Polydor</company> <price>8.20</price> <year>1971</year>

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td>.</td> </table> </body> </html> </xsl:template> </xsl:stylesheet> RESULT

XSLT Example Explained Since an XSL style sheet is an XML document, it always begins with the XML declaration: <?xml version="1.0" encoding="UTF-8"?>. The next element, <xsl:stylesheet>, defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes). The <xsl:template> element defines a template. The match="/" attribute associates the template with the root of the XML source document. The content inside the <xsl:template> element defines some HTML to write to the output. The last two lines define the end of the template and the end of the style sheet. The result from this example was a little disappointing, because no data was copied from the XML document to the output. In the next chapter you will learn how to use the <xsl:value-of> element to select values from the XML elements.

XSLT <xsl:value-of> Element The <xsl:value-of> element is used to extract the value of a selected node. The <xsl:value-of> Element The <xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation:

<. xml version="1. 0" encoding="UTF-8" <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>     </tr>     <tr>       <td><xsl:value-of select="catalog/cd/title"/></td>       <td><xsl:value-of select="catalog/cd/artist"/></td>     </tr>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet>

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </table> </body> </html> </xsl:template> </xsl:stylesheet> RESULT

XSLT <xsl:value-of> Element Example Explained Note: The select attribute, in the example above, contains an XPath expression. An XPath expression works like navigating a file system; a forward slash (/) selects subdirectories. The result from the example above was a little disappointing; only one line of data was copied from the XML document to the output. In the next chapter you will learn how to use the <xsl:for-each> element to loop through the XML elements, and display

XSLT <xsl:for-each> Element The <xsl:for-each> element allows you to do looping in XSLT. The <xsl:for-each> Element The XSL <xsl:for-each> element can be used to select every XML element of a specified node-set: Note: The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories.

<. xml version="1. 0" encoding="UTF-8" <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>     </tr>     <xsl:for-each select="catalog/cd">     <tr>       <td><xsl:value-of select="title"/></td>       <td><xsl:value-of select="artist"/></td>     </tr>     </xsl:for-each>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet>

RESULT

XSLT <xsl:for-each> Element Filtering the Output We can also filter the output from the XML file by adding a criterion to the select attribute in the <xsl:for-each> element. <xsl:for-each select="catalog/cd[artist='Bob Dylan']"> Legal filter operators are: =  (equal) != (not equal) < less than > greater than Take a look at the adjusted XSL style sheet:

XSLT <xsl:for-each> Element <xsl:for-each select="catalog/cd[artist='Bob Dylan']">     <tr>       <td><xsl:value-of select="title"/></td>       <td><xsl:value-of select="artist"/></td>     </tr>     </xsl:for-each>

XSLT <xsl:sort> Element The <xsl:sort> element is used to sort the output. Where to put the Sort Information To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each> element in the XSL file:

<. xml version="1. 0" encoding="UTF-8" <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>     </tr>     <xsl:for-each select="catalog/cd">       <xsl:sort select="artist"/>       <tr>         <td><xsl:value-of select="title"/></td>         <td><xsl:value-of select="artist"/></td>       </tr>     </xsl:for-each>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet>

Note: The select attribute indicates what XML element to sort on. RESULT Note: The select attribute indicates what XML element to sort on.

XSLT <xsl:if> Element The <xsl:if> element is used to put a conditional test against the content of the XML file. The <xsl:if> Element To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document. Syntax <xsl:if test="expression">   ...some output if the expression is true... </xsl:if>

<. xml version="1. 0" encoding="UTF-8" <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>       <th>Price</th>     </tr>     <xsl:for-each select="catalog/cd">       <xsl:if test="price > 10">         <tr>           <td><xsl:value-of select="title"/></td>           <td><xsl:value-of select="artist"/></td>           <td><xsl:value-of select="price"/></td>         </tr>       </xsl:if>     </xsl:for-each>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet> Note: The value of the required test attribute contains the expression to be evaluated. The code above will only output the title and artist elements of the CDs that has a price that is higher than 10.

XSLT <xsl:choose> Element The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests. The <xsl:choose> Element Syntax <xsl:choose>   <xsl:when test="expression">     ... some output ...   </xsl:when>   <xsl:otherwise>     ... some output ....   </xsl:otherwise> </xsl:choose>

<. xml version="1. 0" encoding="UTF-8" <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>     </tr>     <xsl:for-each select="catalog/cd">     <tr>       <td><xsl:value-of select="title"/></td>       <xsl:choose>         <xsl:when test="price > 10">           <td bgcolor="#ff00ff">           <xsl:value-of select="artist"/></td>         </xsl:when>         <xsl:otherwise>           <td><xsl:value-of select="artist"/></td>         </xsl:otherwise>       </xsl:choose>     </tr>     </xsl:for-each>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet>

RESULT

XSLT <xsl:apply-templates> Element The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes. The <xsl:apply-templates> Element If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed. Look at the following XSL style sheet:

<. xml version="1. 0" encoding="UTF-8" <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <xsl:apply-templates/>   </body>   </html> </xsl:template> <xsl:template match="cd">   <p>   <xsl:apply-templates select="title"/>   <xsl:apply-templates select="artist"/>   </p> </xsl:template> <xsl:template match="title">   Title: <span style="color:#ff0000">   <xsl:value-of select="."/></span>   <br /> </xsl:template> <xsl:template match="artist">   Artist: <span style="color:#00ff00">   <xsl:value-of select="."/></span>   <br /> </xsl:template> </xsl:stylesheet>

RESULT

XSLT - On the Client XSLT can be used to transform the document to XHTML in your browser. A JavaScript Solution In the previous chapters we have explained how XSLT can be used to transform a document from XML to XHTML. We did this by adding an XSL style sheet to the XML file and let the browser do the transformation.Even if this works fine, it is not always desirable to include a style sheet reference in an XML file (e.g. it will not work in a non XSLT aware browser.) A more versatile solution would be to use a JavaScript to do the transformation. By using a JavaScript, we can: do browser-specific testing use different style sheets according to browser and user needs That is the beauty of XSLT! One of the design goals for XSLT was to make it possible to transform data from one format to another, supporting different browsers and different user needs.

XML File <?xml version="1.0" encoding="UTF-8"?> <catalog>   <cd>     <title>Empire Burlesque</title>     <artist>Bob Dylan</artist>     <country>USA</country>     <company>Columbia</company>     <price>10.90</price>     <year>1985</year>   </cd> . . </catalog>

XSL File <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th style="text-align:left">Title</th>       <th style="text-align:left">Artist</th>     </tr>     <xsl:for-each select="catalog/cd">     <tr>       <td><xsl:value-of select="title" /></td>       <td><xsl:value-of select="artist" /></td>     </tr>     </xsl:for-each>   </table> </xsl:template> </xsl:stylesheet>

Transforming XML to XHTML in the Browser <!DOCTYPE html> <html> <head> <script> function loadXMLDoc(filename) { if (window.ActiveXObject)   {   xhttp = new ActiveXObject("Msxml2.XMLHTTP");   } else   {   xhttp = new XMLHttpRequest();   } xhttp.open("GET", filename, false); try {xhttp.responseType = "msxml-document"} catch(err) {} // Helping IE11 xhttp.send(""); return xhttp.responseXML; }

Transforming XML to XHTML in the Browser function displayResult() { xml = loadXMLDoc("cdcatalog.xml"); xsl = loadXMLDoc("cdcatalog.xsl"); // code for IE if (window.ActiveXObject || xhttp.responseType == "msxml-document")   {   ex = xml.transformNode(xsl);   document.getElementById("example").innerHTML = ex;   } // code for Chrome, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument)   {   xsltProcessor = new XSLTProcessor();   xsltProcessor.importStylesheet(xsl);   resultDocument = xsltProcessor.transformToFragment(xml, document);   document.getElementById("example").appendChild(resultDocument);   } } </script> </head> <body onload="displayResult()"> <div id="example" /> </body> </html>

RESULT

Transforming XML to XHTML in the Browser The loadXMLDoc() function does the following: Create an XMLHttpRequest object Use the open() and send() methods of the XMLHttpRequest object to send a request to a server Get the response data as XML data The displayResult() function is used to display the XML file styled by the XSL file: Load XML and XSL files Test what kind of browser the user has If Internet Explorer: Use the transformNode() method to apply the XSL style sheet to the xml document Set the body of the current document (id="example") to contain the styled xml document If other browsers: Create a new XSLTProcessor object and import the XSL file to it Use the transformToFragment() method to apply the XSL style sheet to the xml document

XSLT - On the Server To make XML data available to all kind of browsers, we can transform the XML document on the SERVER and send it  back to the browser as XHTML. A Cross Browser Solution In the previous chapter we explained how XSLT can be used to transform a document from XML to XHTML in the browser. We used a JavaScript and an XML parser for the transformation. However, this will not work in a browser that doesn't have an XML parser.To make XML data available to all kind of browsers, we can transform the XML document on the server and send back to the browser as XHTML. That's another beauty of XSLT. One of the design goals for XSLT was to make it possible to transform data from one format to another on a server, returning readable data to all kinds of browsers.

XML File <?xml version="1.0" encoding="UTF-8"?> <catalog>   <cd>     <title>Empire Burlesque</title>     <artist>Bob Dylan</artist>     <country>USA</country>     <company>Columbia</company>     <price>10.90</price>     <year>1985</year>   </cd> . . </catalog>

XSL FILE <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th style="text-align:left">Title</th>       <th style="text-align:left">Artist</th>     </tr>     <xsl:for-each select="catalog/cd">     <tr>       <td><xsl:value-of select="title" /></td>       <td><xsl:value-of select="artist" /></td>     </tr>     </xsl:for-each>   </table> </xsl:template> </xsl:stylesheet>

PHP Code: Transform XML to XHTML on the Server <?php // Load XML file $xml = new DOMDocument; $xml->load('cdcatalog.xml'); // Load XSL file $xsl = new DOMDocument; $xsl->load('cdcatalog.xsl'); // Configure the transformer $proc = new XSLTProcessor; // Attach the xsl rules $proc->importStyleSheet($xsl); echo $proc->transformToXML($xml); ?>

ASP Code: Transform XML to XHTML on the Server <% 'Load XML file set xml = Server.CreateObject("Microsoft.XMLDOM") xml.async = false xml.load(Server.MapPath("cdcatalog.xml")) 'Load XSL file set xsl = Server.CreateObject("Microsoft.XMLDOM") xsl.async = false xsl.load(Server.MapPath("cdcatalog.xsl")) 'Transform file Response.Write(xml.transformNode(xsl)) %>