Download presentation
Presentation is loading. Please wait.
1
Session II Chapter 2 - XSLT http://www.profburnett.com
CMP 051 XML Introduction Session II Chapter 2 - XSLT
2
Outline XSL Languages Transforming XML with XSLT
Beginning an XSLT Style Sheet Creating a Root Template Outputting HTML Outputting Values Looping over Nodes Processing Nodes Conditionally Adding Condition Clauses Sorting Nodes Before Processing Generating Output Attributes Creating and Applying Templates XSLT - On the Client XSLT - On the Server XSLT Exercise 1 8/1/2014 Copyright © Carl M. Burnett
3
XSL Languages CSS = Style Sheets for HTML XSL = Style Sheets for XML
XSL consists of four parts: XSLT - a language for transforming XML documents XPath - a language for navigating in XML documents XSL-FO - a language for formatting XML documents (discontinued in 2013) XQuery - a language for querying XML documents 8/1/2014 Copyright © Carl M. Burnett
4
Transforming XML with XSLT
<xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:transform version="1.0" xmlns:xsl=" 8/1/2014 Copyright © Carl M. Burnett
5
Transforming XML with XSLT
Create an XSL Style Sheet <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/"> <html> <body> <h2>2016 Female Songs of the Year</h2> <table border="1"> <tr bgcolor="#9acd32"> <th style="text-align:left">Title</th> <th style="text-align:left">Artist</th> <th style="text-align:left">Album</th> </tr> <xsl:for-each select="DocumentElement/Song"> <tr> <td><xsl:value-of select="Track_Name"/></td> <td><xsl:value-of select="Artist_Name"/></td> <td><xsl:value-of select="Album_Name"/></td> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> 8/1/2014 Copyright © Carl M. Burnett
6
Transforming XML with XSLT
Link the XSL Style Sheet to the XML Document <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="2016_Female_Songs_of_the_Year.xsl"?> <DocumentElement> <Song> <Track_Name>Always</Track_Name> <Artist_Name>Samantha Jade</Artist_Name> <Album_Name>Nine</Album_Name> <Disc_Number>1</Disc_Number> <Track_Number>1</Track_Number> <Track_Duration>181880</Track_Duration> <Username>newmediaman</Username> <Data_Added> T11:48:05Z</Data_Added> <YouTube_Video> <_x003F_Spotify_URI>spotify:track:13j7S1J1MVDY3z2Ljl9O1H</_x003F_Spotify_URI> </Song> ..... View Female Songs of the Year.xsl Song Collection 8/1/2014 Copyright © Carl M. Burnett
7
Beginning an XSLT Style Sheet
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" version="1.0"> </xsl:stylesheet> 8/1/2014 Copyright © Carl M. Burnett
8
Creating a Root Template
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:template match="/"> </xsl:template> </xsl:stylesheet> 8/1/2014 Copyright © Carl M. Burnett
9
Outputting HTML 8/1/2014 Copyright © Carl M. Burnett
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html> <head> <title>Wonders of the World</title> </head> <body> <p align="center"><img src="herodotus.jpg" width="120" height="171"/></p> <p>The famous Greek historian Herodotus wrote of seven great architectural achievements. And although his writings did not survive, he planted seeds for what has become the list of the <strong>Seven Wonders of the Ancient World</strong>.</p> </body> </html> </xsl:template> </xsl:stylesheet> 8/1/2014 Copyright © Carl M. Burnett
10
Outputting Values <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html> <head> <title>Wonders of the World</title> </head> <body> <h1>Seven Wonders of the Ancient World</h1> <p>The <xsl:value-of select="ancient_wonders/wonder/name"/> is one of the wonders.</p> </body> </html> </xsl:template> </xsl:stylesheet> 8/1/2014 Copyright © Carl M. Burnett
11
Looping over Nodes 8/1/2014 Copyright © Carl M. Burnett
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html><head><title>Wonders of the World</title></head> <body> <h1>Seven Wonders of the Ancient World</h1> <table border="1"> <tr> <th>Wonder Name</th> <th>Location</th> <th>Height</th> </tr> <xsl:for-each select="ancient_wonders/wonder"> <td><strong><xsl:value-of <td><xsl:value-of select="location"/></td> <td><xsl:value-of select="height"/></td> </xsl:for-each> </table> </body></html> </xsl:template> </xsl:stylesheet> 8/1/2014 Copyright © Carl M. Burnett
12
Processing Nodes Conditionally
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html><head><title>Wonders of the World</title></head> <body> <h1>Seven Wonders of the Ancient World</h1> <table border="1"><tr><th>Wonder Name</th><th>Location</th><th>Height</th></tr> <xsl:for-each select="ancient_wonders/wonder"> <tr><td><strong><xsl:value-of <xsl:if <br/>(<em><xsl:value-of </xsl:if> </td> <td><xsl:value-of select="location"/></td> <td><xsl:value-of select="height"/></td> </tr> </xsl:for-each> </table> </body></html> </xsl:template> </xsl:stylesheet> 8/1/2014 Copyright © Carl M. Burnett
13
Adding Condition Clauses
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html><head><title>Wonders of the World</title></head> <body> <h1>Seven Wonders of the Ancient World</h1> <table border="1"><tr><th>Wonder Name</th><th>Location</th><th>Height</th></tr> <xsl:for-each select="ancient_wonders/wonder"> <tr><td><strong><xsl:value-of <xsl:if <br/>(<em><xsl:value-of </xsl:if> </td> <td><xsl:value-of select="location"/></td> <td> <xsl:choose> <xsl:when test="height != 0"> <xsl:value-of select="height"/> </xsl:when> <xsl:otherwise> unknown </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body></html> </xsl:template> </xsl:stylesheet> 8/1/2014 Copyright © Carl M. Burnett
14
Sorting Nodes Before Processing
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html><head><title>Wonders of the World</title></head> <body> <h1>Seven Wonders of the Ancient World</h1> <table border="1"><tr><th>Wonder Name</th><th>Location</th><th>Height</th></tr> <xsl:for-each select="ancient_wonders/wonder"> <xsl:sort select="height" order="descending" data-type="number" /> <tr><td><strong><xsl:value-of <xsl:if <br/>(<em><xsl:value-of </xsl:if> </td> <td><xsl:value-of select="location"/></td> <td> <xsl:choose> <xsl:when test="height != 0"> <xsl:value-of select="height"/> </xsl:when> <xsl:otherwise> unknown </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body></html> </xsl:template> </xsl:stylesheet> 8/1/2014 Copyright © Carl M. Burnett
15
Generating Output Attributes
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html><head><title>Wonders of the World</title></head> <body> <h1>Seven Wonders of the Ancient World</h1> <table border="1"><tr><th>Wonder Name</th><th>Location</th><th>Height</th></tr> <xsl:for-each select="ancient_wonders/wonder"> <xsl:sort select="height" order="descending" data-type="number" /> <tr><td> <a><xsl:attribute name="href">#<xsl:value-of <strong><xsl:value-of <xsl:if <br/>(<em><xsl:value-of </xsl:if> </td> <td><xsl:value-of select="location"/></td> <td> <xsl:choose> <xsl:when test="height != 0"> <xsl:value-of select="height"/> </xsl:when> <xsl:otherwise> unknown </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> <xsl:comment>Insert section for named references of each wonder</xsl:comment> </body></html> </xsl:template> </xsl:stylesheet> 8/1/2014 Copyright © Carl M. Burnett
16
Creating and Applying Templates
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html><head><title>Wonders of the World</title></head> <body> <h1>Seven Wonders of the Ancient World</h1> <h2>Overview</h2> <table border="1"> <tr> <th>Wonder Name</th> <th>Location</th> <th>Height</th> </tr> <xsl:for-each select="ancient_wonders/wonder"> <xsl:sort select="height" order="descending" data-type="number" /> 8/1/2014 Copyright © Carl M. Burnett
17
Creating and Applying Templates
<tr> <td> <a><xsl:attribute name="href">#<xsl:value-of <strong><xsl:value-of <xsl:apply-templates </td> <td><xsl:value-of select="location"/></td> <xsl:choose> <xsl:when test="height != 0"> <xsl:value-of select="height"/> </xsl:when> <xsl:otherwise> unknown </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> 8/1/2014 Copyright © Carl M. Burnett
18
Creating and Applying Templates
<h2>History</h2> <xsl:for-each select="ancient_wonders/wonder"> <xsl:sort select="height" order="descending" data-type="number" /> <a><xsl:attribute name="name"><xsl:value-of <xsl:value-of <xsl:apply-templates <br /><br /> </xsl:for-each> </body></html> </xsl:template> <xsl:template (<em><xsl:value-of select="."/></em>) </xsl:stylesheet> 8/1/2014 Copyright © Carl M. Burnett
19
Creating and Applying Templates
<tr> <td> <a><xsl:attribute name="href">#<xsl:value-of <strong><xsl:value-of <xsl:apply-templates </td> <td><xsl:value-of select="location"/></td> <xsl:choose> <xsl:when test="height != 0"> <xsl:value-of select="height"/> </xsl:when> <xsl:otherwise> unknown </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> 8/1/2014 Copyright © Carl M. Burnett
20
Creating and Applying Templates
<h2>History</h2> <xsl:for-each select="ancient_wonders/wonder"> <xsl:sort select="height" order="descending" data-type="number" /> <a><xsl:attribute name="name"><xsl:value-of <xsl:value-of <xsl:apply-templates <br /><br /> </xsl:for-each> </body></html> </xsl:template> <xsl:template (<em><xsl:value-of select="."/></em>) </xsl:stylesheet> 8/1/2014 Copyright © Carl M. Burnett
21
XSLT - On the Client A JavaScript Solution – Using an AJAX Call
<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; } 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> 8/1/2014 Copyright © Carl M. Burnett
22
XSLT - On the Client 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 8/1/2014 Copyright © Carl M. Burnett
23
XSLT - On the Server AMP Stack – PHP/PERL/Python Code: Transform XML to XHTML on the Server .net - ASP Code: Transform XML to XHTML on the Server MEAN Stack – Angular.js: Transform XML to XHTML on the Server 8/1/2014 Copyright © Carl M. Burnett
24
XSLT Exercise 1 Create an XSL Style Sheet (Slide 5)
Create XML with xsl style sheet connected. (Slide 6) Preview Transformed XML HTML file in browser. Link files to your index webpage. (Files to xsl, xml, transformed xml file) Post to live server. Preview on live site. 8/1/2014 Copyright © Carl M. Burnett
25
Review XSL Languages Transforming XML with XSLT
Beginning an XSLT Style Sheet Creating a Root Template Outputting HTML Outputting Values Looping over Nodes Processing Nodes Conditionally Adding Condition Clauses Sorting Nodes Before Processing Generating Output Attributes Creating and Applying Templates XSLT - On the Client XSLT - On the Server XSLT Exercise 1 Next – Chapter 6 – Creating a DTD 8/1/2014 Copyright © Carl M. Burnett
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.