Recycled from Gill Windall’s notes

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.
CG0119 Web Database Systems Parsing XML: using SimpleXML & XSLT.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 12 Extensible Stylesheet Language Transformations : XSLT.
XSLT 11-Apr-17.
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
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.
Introduction to XSLT. What is XML? Design Goals of XML XML Format XML Declaration ElementsAttributes.
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.
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.
Internet Technologies XSLT Processing XML using XSLT Using XPath Escaping to Java.
XML - XSLT © 2012 t he University of Greenwich 1 XML Processing with XSLT Kevin McManus
Tutorial 11 Creating XML Document
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
Object Oriented Programming III1 XSLT Processing XML using XSLT Using XPath.
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
Internet Technologies1 XSLT Processing XML using XSLT Using XPath.
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.
Working with Namespaces Combining XML Vocabularies in a Compound Document.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.
XSLT for Data Manipulation By: April Fleming. What We Will Cover The What, Why, When, and How of XSLT What tools you will need to get started A sample.
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
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,
Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:
XML About XML Things to be known Related Technologies XML DOC Structure Exploring XML.
Tutorial 1: XML Creating an XML Document. 2 Introducing XML XML stands for Extensible Markup Language. A markup language specifies the structure and content.
XML TUTORIAL Portions from w3 schools By Dr. John Abraham.
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.
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.
Lecture 11 XSL Transformations (part 1: Introduction)
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.
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.
XML Design Goals 1.XML must be easily usable over the Internet 2.XML must support a wide variety of applications 3.XML must be compatible with SGML 4.It.
1 Tutorial 11 Creating an XML Document Developing a Document for a Cooking Web Site.
XP New Perspectives on XML, 2 nd Edition Tutorial 8 1 TUTORIAL 8 CREATING ELEMENT GROUPS.
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.
Unit 3 — Advanced Internet Technologies Lesson 11 — Introduction to XSL.
More XML XPATH, XSLT CS 431 – February 23, 2005 Carl Lagoze – Cornell University.
 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.
1 XMLXSL(T) and CSS NOEA/PQC (rev. fen) 2007 Stylesheets CSS:Cascading Style Sheets XSL(T):eXtended Stylesheet Language (Transformations)
XSLT: How Do We Use It? Nancy Hallberg Nikki Massaro Kauffman.
ACG 4401 XSLT Extensible Stylesheet Language for Transformations Presenting XML and XBRL.
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.
XML - XSLT © 2015 University of Greenwich 1 XML Processing with XSLT e Xtensible Stylesheet Language Transforms Dr Kevin McManus
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.
Session IV Chapter 14 – Chapter 14 – XSLThttp://
XSLT: The XML transformation language
ACG 4401 XSLT Extensible Stylesheet Language for Transformations
Applying eXtensible Style Sheets (XSL)
Presentation transcript:

Recycled from Gill Windall’s notes XML 3 XSLT Recycled from Gill Windall’s notes © 2004 the University of Greenwich

The need for style sheets with XML XML defined tags describe document content but not presentation Presentation information can be added using of style sheets. XML document + style sheet = presentable document There are two main style sheet languages - both are W3C standards CSS - Cascading Style Sheets XSL - eXtensible Stylesheet Language (XML Stylesheet Language) CSS is a more mature (in Internet terms) standard XSL itself consists of two languages XSL Transformations (XSLT) can restructure an XML document - often used to convert XML into HTML or XHTML XSL Formatting Objects (XSL-FO) defines how elements are displayed. XSLT and XSL-FO can be used independently XSL-FO has some opponents XSLT is more stable and widely used and is what is covered here. © 2004 the University of Greenwich

The difference between CSS and XSL XSL - can restructure an XML document and then format it for display Spot the difference? plain XML XML rendered XSL-FO or CSS XSLT XML transformed and rendered plain XML XML transformed

XSLT is a transformation language XSLT is very different from CSS XSLT can transform an XML document into any other format (e.g. another XML document, HTML, PDF, plain text). For display in a browser it makes sense for the output document to be in HTML or XHTML format because that is what browsers can display. XML document XSLT processor HTML document XSLT style sheet © 2004 the University of Greenwich

the University of Greenwich XSLT Processing Does the XSLT processing have to happen at the client side like CSS? No. It often makes sense to do the transformation at the server end and just deliver straight HTML across the web to a variety of browsers Sablotron from Ginger Alliance is an XSLT processor available with Perl and PHP language bindings Xalan from Apache is a Java XSLT processor © 2004 the University of Greenwich

the University of Greenwich XSLT Browsers Internet Explorer MSXML.dll shipped with IE 5 and 5.5 is non-standard IE6 uses MSXML3.dll is claimed to be 100% W3C compatible You can upgrade to MSXML3.dll by installing it Netscape 6 supports some XSLT 7 supports the official W3C XSLT recommendation Mozilla Uses the open source TransforMiiX module Opera 6+ supports some XML but not XSLT policy decision, see ‘Formatting Objects Considered Harmful’ http://people.opera.com/howcome/1999/foch.html The examples here are tested with IE6, Netscape 6+ and Mozilla (Firefox) © 2004 the University of Greenwich

goodBookseg1.xml link to XSLT style sheet default rating “ok” <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="eg1.xsl"?> <!DOCTYPE recommended_books SYSTEM "goodBooks.dtd"> <recommended_books> <book rating="good"> <author> <firstname>Stephen</firstname> <surname>Spainhour</surname> </author> <title>Webmaster in a Nutshell</title> <year_published>1999</year_published> <publisher>O'Reilly</publisher> <course>COMP1037</course> <recommended_by> <firstname>Gill</firstname> <surname>Windall</surname> </recommended_by> </book> <book> <firstname>Benoît</firstname> <surname>Marchal</surname> <title>Applied XML Solutions</title> <year_published>2000</year_published> <publisher>Sams</publisher> ...... link to XSLT style sheet default rating “ok” character reference goodBookseg1.xml

the University of Greenwich eg1.xsl xsl: namespace <?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> <h2>From eg1.xsl</h2> <i> <xsl:value-of select="."/> </i> </html> </xsl:template> </xsl:stylesheet> © 2004 the University of Greenwich

goodBooks.eg1.xml

the University of Greenwich eg1.xsl XML style sheets are written in XML Start with an XML declaration Must include a namespace declaration <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> XSLT elements have the form… name of the XSL element – in this case template xsl: namespace <xsl:template match="/"> element content </xsl:template> end tag empty elements are also possible, e.g. <xsl:value-of /> Attributes in normal XML format © 2004 the University of Greenwich

XSLT language constructs XSLT is a substantially declarative language as opposed to procedural In declarative languages you specify what you want as opposed to how you want to achieve it XSLT has more in common with languages such as SQL and Prolog than it does with procedural programming languages such as C or Java although XSLT does support conditional and iterative constructs XSLT consists of a series of rules that match with things in the source document specifies what should be output when the match occurs © 2004 the University of Greenwich

XSLT templates The view of the input document is a hierarchical tree consisting of nodes root node book recommended_books XSLT rules are specified as templates to be applied to nodes © 2004 the University of Greenwich

the University of Greenwich xsl:template XSLT stylesheets consist of one of more templates. The XSLT processor will attempt to match the templates against the elements in the XML document using a pattern specified by the match= attribute. If the template matches the action (content of the template) will take effect. pattern to match – in this case the root element <xsl:template match="/"> <html> <h2>From eg1.xsl</h2> <i> <xsl:value-of select="."/> </i> </html> </xsl:template> action of the template is inserted into the output document © 2004 the University of Greenwich

the University of Greenwich xsl:value-of An instruction to insert the value of something from the source document into the output document What gets inserted is determined by the select attribute <xsl:value-of select="."/> “.” means the current node of the input document in this case the whole document With client side processing you never see this HTML if you select "view source" you'll see the original XML document this can make debugging somewhat difficult <html><h2>From eg1.xsl</h2><i>Stephen Spainhour Webmaster in a Nutshell 1999 O'Reilly COMP1037 Gill Windall Benoît Marchal Applied XML Solutions 2000 Sams COMP1037 Kevin McManus Subrahmanyam Allamaraju Java Server Programming 2000 Wrox COMP1086 Liz Bacon Jennifer Neiderst Web Design in a Nutshell 1999 O'Reilly COMP1037 Gill Windall Daniel Berg Advanced Techniques for Java Developers 1999 Wiley COMP1030 Liz Bacon</i></html> © 2004 the University of Greenwich

eg2.xsl - xsl:apply-templates <?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> <h2>From eg2.xsl</h2> <xsl:apply-templates select="//author"/> </html> </xsl:template> <xsl:template match="author"> <p> <xsl:value-of select="."/> </p> <hr /> </xsl:stylesheet> apply a template matching author template matching author elements © 2004 the University of Greenwich

Note how you get both author/firstname and author/surname author elements from goodBookseg2.xml © 2004 the University of Greenwich

the University of Greenwich xsl:apply-templates xsl:apply-templates is an instruction that tells the XSLT processor to look for templates that match the pattern specified by the select attribute <xsl:apply-templates select="pattern"/> In eg2.xsl the pattern is //author any elements called author that exist at any level below the root element in the source XML document The pattern is an XPath expression XPath A pattern matching language used to address specific parts of an XML document. Used within XSLT and other XML technologies such as XLink and XPointer © 2004 the University of Greenwich

xsl:apply-templates <xsl:apply-templates select="//author"/> select="//author" returns a set of nodes corresponding to all of the author nodes in the document xsl:apply-templates then looks for templates to apply to each of them in turn Match author elements <xsl:template match="author"> <p> <xsl:value-of select="."/> </p> <hr /> </xsl:template> action part of the template specifying what to output this template will match in turn with each of the author element selected by the previous xsl:apply-templates © 2004 the University of Greenwich

the University of Greenwich XPath Patterns are used by… the select= attribute of the xsl:apply-templates instruction <xsl:apply-templates select="//author"/> the match= attribute of the xsl:template element <xsl:template match="author"> Xpath nodes correspond to elements, attributes and text in the XML document XPath patterns are structured similarly to the paths to files and directories book/author/firstname would match with the “firstname” elements within “author” within “book” When processed Xpath expressions return a data object of one of the following types: Node set, String, Boolean, Number © 2004 the University of Greenwich

XPath abbreviated operators Meaning Examples Meaning of example / child book/author author elements that are directly contained within book elements /book / at the start means the root node so this would match book elements directly below the root node // recursive descent // all elements anywhere below the root //author all author elements anywhere below the root . current node .//name all name elements anywhere below the current element @ attribute book/@rating rating attributes of the book element * wildcard //* all elements anywhere below root © 2004 the University of Greenwich

the University of Greenwich Quick Quiz <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><h2>From Quiz a)</h2> <xsl:apply-templates select="//firstname"/> </html> </xsl:template> <xsl:template match="firstname"> <p><xsl:value-of select="."/></p><hr/> </xsl:stylesheet> what will appear in a browser when these style sheets are applied to goodBooks.xml? <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><h2>From Quiz b)</h2> <xsl:apply-templates select="//firstname"/> </html> </xsl:template> <xsl:template match="surname"> <p><xsl:value-of select="."/></p><hr/> </xsl:stylesheet> © 2004 the University of Greenwich

the University of Greenwich Quick Quiz <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><h2> From Quiz c)</h2><ol> <xsl:apply-templates select="//title"/> </ol></html> </xsl:template> <xsl:template match="title"> <li><xsl:value-of select="." /></li> </xsl:stylesheet> …and with these style sheets? <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><h2> From Quiz d)</h2><ul> <xsl:apply-templates select="//author/surname"/> </ul></html> </xsl:template> <xsl:template match="surname"> <li><xsl:value-of select="."/></li> </xsl:stylesheet> © 2004 the University of Greenwich

the University of Greenwich Quick Quiz <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><h2>From Quiz e)</h2> <xsl:apply-templates select="//book"/> </html> </xsl:template> <xsl:template match="book"> <p> <xsl:apply-templates select="author"/> <xsl:apply-templates select="title"/> - rated as : <xsl:value-of select="@rating"/> </p> <xsl:template match="author"> <xsl:value-of select="."/> <xsl:template match="title"> - <i><xsl:value-of select="."/></i> </xsl:stylesheet> …and finally © 2004 the University of Greenwich

the University of Greenwich eg3.xsl © 2004 the University of Greenwich

eg3.xsl start an HTML table for each book sort by title <?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 style="font-family:arial"> <h3>From eg3.xsl - books in title order</h3> <table border="1" cellpadding="3"> <thead> <tr><th>title</th> <th>author</th></tr> </thead><tbody> <xsl:for-each select="//book"> <xsl:sort order="ascending" select="title"/> <tr> <xsl:apply-templates select="title"/> <xsl:apply-templates select="author"/> </tr> </xsl:for-each> </tbody> </table> </body> </html> </xsl:template> <xsl:template match="*"> <td> <xsl:value-of select="."/> </td> </xsl:stylesheet> eg3.xsl start an HTML table for each book sort by title output title then author end of the table match anything output as a table column

the University of Greenwich eg3.xsl It's possible to do things with XSLT that aren't remotely possible with CSS! This example reorders the list of books and for each book changes the order of elements (author comes before title in the XML document) xsl:for-each is a loop construct much as you would expect in other procedural programming languages <xsl:for-each select="//book"> xsl:sort allows you to sort elements <xsl:sort order="ascending" select="title"/> Can also specify descending order pattern to match © 2004 the University of Greenwich

the University of Greenwich Quick Quiz What order would you expect the books to appear in if the code were changed to: <xsl:for-each select="//book"> <xsl:sort order="descending" select="author/surname"/> © 2004 the University of Greenwich

eg4.xsl This is much like the previous example <?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 style="font-family:arial"> <h3>From eg4.xsl</h3><h4>List in title order</h4><ul> <xsl:for-each select="//book"> <xsl:sort order="ascending" select="title"/> <li><xsl:value-of select="title"/> / <xsl:value-of select="course"/></li> </xsl:for-each> </ul> <h4>List in course order</h4> <table border="1" cellpadding="3"> <thead><tr><th>course</th><th>title</th></tr></thead><tbody> <xsl:sort order="ascending" select="course"/> <tr><td><xsl:value-of select="course"/> </td><td><xsl:value-of select="title"/> </td></tr> </tbody></table> <h4>Full book details</h4> <xsl:apply-templates select="//book"/> </body></html> </xsl:template> <xsl:template match="book"> rating: <xsl:value-of select="@rating"/> <br /> <xsl:for-each select="*"> <xsl:value-of select="local-name()"/> : <xsl:value-of select="."/><br /> <hr /> </xsl:stylesheet> eg4.xsl This is much like the previous example output the rating attribute output the name of the element

eg4.xsl Spot the difference?

the University of Greenwich eg4.xsl Elements in the XML document are repeated with different formatting in the output document Reading element attribute values using xsl:value-of <xsl:value-of select="@rating"/> Using local_name() to return the element name from xsl:value-of <xsl:value-of select="local-name()"/>: Is that all? No - there are many more features included in XSLT. The next example demonstrates some A detailed explanation is omitted for brevity but you should have little difficulty figuring out how it works. © 2004 the University of Greenwich

eg5.xsl book rating attribute value converted to a * rating anchor tags link to the full book details book titles in bold

the University of Greenwich eg5.xsl <?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 style="font-family:arial"> <h2>From eg5.xsl</h2> <h3>List in title order</h3> <ul> <xsl:for-each select="//book"> <xsl:sort order="ascending" select="title"/> <li> <xsl:apply-templates select="title"/> / <xsl:apply-templates select="course"/> / <xsl:apply-templates select="publisher"/> / <span style="color:#FF9900;font-weight:bold;font-size:20pt"> <xsl:apply-templates select="@rating"/> </span> </li> </xsl:for-each> </ul> © 2004 the University of Greenwich

eg5.xsl assign the value of the course element to a variable <h3>List in course order</h3> <table border="1" cellpadding="3"> <thead> <tr> <th>course</th> <th>title</th> <th>stars</th> </tr> </thead> <tbody> <xsl:for-each select="//book"> <xsl:sort order="ascending" select="course"/> <td> <xsl:variable name="courseCode" select="course"/> <xsl:value-of select="$courseCode"/> <xsl:if test="$courseCode = 'COMP1037'"> WAT</xsl:if> </td> <xsl:apply-templates select="title"/> <xsl:apply-templates select="@rating"/> </xsl:for-each> </tbody> </table> assign the value of the course element to a variable test the value of the variable

eg5.xsl call a named template <h3>Full book details</h3> <xsl:for-each select="//book"> <xsl:call-template name="full-details"/> </xsl:for-each> </body></html> </xsl:template> <!-- format title as a link --> <xsl:template match="title"> <xsl:element name="a"> <xsl:attribute name="href">#<xsl:value-of select="."/></xsl:attribute> <xsl:value-of select="."/> </xsl:element> <!-- output rating attribute as a star rating --> <xsl:template match="@rating"> <xsl:choose> <xsl:when test=". = 'ok'"> * </xsl:when> <xsl:when test=". = 'good'"> ** <xsl:when test=". = 'excellent'"> *** <xsl:otherwise> </xsl:otherwise> </xsl:choose> eg5.xsl call a named template form an anchor tag linking to a named anchor in the output document convert the value of the book rating attribute to stars

eg5.xsl create a named anchor with the name of the book <!-- full book details --> <xsl:template match="book" name="full-details"> <p> <!-- make the name of the book a target --> <xsl:element name="a"> <xsl:attribute name="name"> <xsl:value-of select="title"/> </xsl:attribute> <b> </b> </xsl:element> <br /> rating : <xsl:value-of select="@rating"/> <xsl:for-each select="*"> <xsl:choose> <xsl:when test="self::title"/> <xsl:otherwise> <br/> <xsl:value-of select="local-name()"/> : <xsl:value-of select="."/> </xsl:otherwise> </xsl:choose> </xsl:for-each> </p> </xsl:template> </xsl:stylesheet> create a named anchor with the name of the book output the title in bold don’t output the title again unabbreviated XPath relative location path

the University of Greenwich eg5.xsl The links (anchors) take the reader to internal named targets e.g. <a href="Webmaster in a Nutshell"/> could just as easily reference external URLs these could be provided in the XML Two different conditional constructs are used xsl:if xsl:choose xsl:when xsl:otherwise forms a case statement Named template full-details operates like a function xsl:param can be used to pass parameters to named templates xsl:variable is used to store the course code value xsl:variable is more like a constant in that it can be assigned only once except in this example! <xsl:value-of select="$courseCode"/> outputs the value of the variable $courseCode rather like an echo statement Unabbreviated XPath location self::title No template is provided for course! the default template is <xsl:value-of select="."/> Combination of XSLT and CSS technologies © 2004 the University of Greenwich

XSLT server side processing XSL processing on the server means that plain HTML (XHTML) is returned to the browser Browser compatibility problems are avoided. A number of server side XSL processing possibilities are available: Java Perl ASP PHP PHP uses the Sablotron XSLT processor from Ginger Alliance. Sablotron is an open source multi-platform XSLT, DOM and Xpath processor built on James Clark’s expat XML parser. © 2004 the University of Greenwich

XSLT processing with PHP Requires that PHP is configured to support XSLT

the University of Greenwich eg6.php create an XSLT processor Set a path to the XML and XSLT <?php error_reporting(15); $xh = xslt_create(); xslt_set_base($xh, 'file:///home/mk05/public_html/web/XML/XSLT/'); $result=xslt_process($xh, 'goodBooks.xml', 'eg6.xsl'); if ($result) { echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; echo $result; } else { echo "There is an error in the XSL transformation...\n"; echo "\tError number: " . xslt_errno($xh) . "\n"; echo "\tError string: " . xslt_error($xh) . "\n"; exit; } xslt_free($xh); ?> apply the XSLT to the XML output the resulting XHTML free resources after use © 2004 the University of Greenwich

the University of Greenwich eg6.php © 2004 the University of Greenwich

the University of Greenwich eg6.php See the PHP documentation for details the current version of Sablotron is 1.0.3 Begin by creating an XSLT processor $xh = xslt_create(); Then pass the XSLT processor and XML containers with the source XML and the XSLT to xslt_process() $result=xslt_process($xh, 'goodBooks.xml', 'eg6.xsl'); The results of processing $result is returned to the browser and the processor resources liberated xslt_free(); The XSLT is the same as Example 3 © 2004 the University of Greenwich

the University of Greenwich Quick Quiz What are the relative merits of processing XSLT client-side or server-side? © 2004 the University of Greenwich

the University of Greenwich eg7.xsl © 2004 the University of Greenwich

eg7.xsl <?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 style="font-family:arial"> <h3>Server side transform from eg7.xsl - data entered intro a form</h3> <xsl:for-each select="//book"> <form action="insert.php" method="post"> <input type="text" size="40" name="txtTitle"> <xsl:attribute name="value"> <xsl:value-of select="title"/> </xsl:attribute> </input> <input type="text" name="txtFirstname"> <xsl:value-of select="author/firstname"/> <input type="text" name="txtSurname"> <xsl:value-of select="author/surname"/> <input type="submit" value="Insert into db" /> </form> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet> eg7.xsl

the University of Greenwich eg7.xsl This example creates an XHTML form for each book in the XML allows the user to choose which book is to be inserted into the MySQL database <xsl:for-each select="//book"> <form action="insert.php" method="post"> <input type="text" size="40" name="txtTitle"> <xsl:attribute name="value"> <xsl:value-of select="title"/> </xsl:attribute> </input> ...... <xsl:for-each> xsl:attribute passes the value of the XML element into an attribute enclosing HTML element The form action is a simple PHP script insert.php that inserts and displays the POST data from the form This XSLT transformation could take place at either the client or the server © 2004 the University of Greenwich

the University of Greenwich Summary CSS and XSL can be used to add presentation details to XML documents. XSL consists of XSLT (transforms) and XSL-FO (formatting objects) We've only looked at XSLT here. XSL is more powerful than CSS it can transform the structure of a document and add CSS styling information XSLT is itself an XML language and uses XML syntax XSLT can transform an XML document into any other text based format e.g. another XML document. For display by a browser XSLT is often used to transform XML into XHTML Transformation may take place at the browser side or the server side so that XHTML is delivered to the browser. XSLT works by matching elements in the source document and specifying the output to generate when a match occurs uses XPath pattern matching © 2004 the University of Greenwich