Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML - XSLT © 2015 University of Greenwich 1 XML Processing with XSLT e Xtensible Stylesheet Language Transforms Dr Kevin McManus

Similar presentations


Presentation on theme: "XML - XSLT © 2015 University of Greenwich 1 XML Processing with XSLT e Xtensible Stylesheet Language Transforms Dr Kevin McManus"— Presentation transcript:

1 XML - XSLT © 2015 University of Greenwich 1 XML Processing with XSLT e Xtensible Stylesheet Language Transforms Dr Kevin McManus http://staffweb.cms.gre.ac.uk/~mk05/web/XML/2/

2 XML - XSLT © 2015 University of Greenwich 2 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

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

4 XML - XSLT © 2015 University of Greenwich 4 XSLT is a Transformation Language XSLT is very different from CSS XSLT is designed to transform an XML document into a different XML document usually an application specific XML to XHTML for display in a browser browsers also understand XML languages SVG and MathML could be another text format such as CSV or JSON XML document XSLT style sheet XHTML document XSLT processor

5 XML - XSLT © 2015 University of Greenwich 5 XSLT Processing Does the XSLT processing have to happen at the client side like CSS? 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 Apache Xalan has Java and C++ bindings The PHP5,.NET and JavaScript DOM object supports XSLT Most Web user agents support XSLT

6 XML - XSLT Stephen Spainhour Webmaster in a Nutshell 1999 O'Reilly COMP1037 Gill Windall Benoît Marchal Applied XML Solutions 2000 Sams COMP1037 Kevin McManus goodBookseg1.xml link to XSLT style sheet default book attribute rating “ok” note the character reference

7 XML - XSLT © 2015 University of Greenwich 7 eg1.xsl From eg1.xsl xsl: namespace

8 XML - XSLT goodBooks.eg1.xml

9 XML - XSLT © 2015 University of Greenwich 9 eg1.xsl XML style sheets are written in XML Start with an XML declaration Must include a namespace declaration XSLT elements have the form… element content Attributes in normal XML format end tag empty elements are also possible, e.g. name of the XSL element – in this case template xsl: namespace

10 XML - XSLT © 2015 University of Greenwich 10 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 not unlike SQL and Prolog XSLT also supports conditional and iterative constructs and functions XSLT consists of a series of rules that match with things in the source document specifies what should be output when the match occurs

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

12 XML - XSLT © 2015 University of Greenwich 12 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. When the template matches the action (content of the template) will take effect. From eg1.xsl action of the template is inserted into the output document pattern to match – in this case the root element

13 XML - XSLT © 2015 University of Greenwich 13 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 “.” 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 From eg1.xsl 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

14 XML - XSLT © 2015 University of Greenwich 14 eg2.xsl - xsl:apply-templates <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> From eg2.xsl template matching author elements apply a template matching author

15 XML - XSLT © 2015 University of Greenwich 15 Note how you get both author/firstname and author/surname author elements from goodBookseg2.xml

16 XML - XSLT © 2015 University of Greenwich 16 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 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

17 XML - XSLT © 2015 University of Greenwich 17 xsl:apply-templates 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 this template will match in turn with each of the author element selected by the previous xsl:apply-templates match author elements action part of the template specifying what to output

18 XML - XSLT © 2015 University of Greenwich 18 XPath Patterns are used by… the select= attribute of the xsl:apply-templates instruction the match= attribute of the xsl:template element 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

19 XML - XSLT © 2015 University of Greenwich 19 XPath abbreviated operators all elements anywhere below root//*wildcard * rating attributes of the book elementbook/@ratingattribute @ all name elements anywhere below the current element.//namecurrent node. all author elements anywhere below the root //author all elements anywhere below the root //recursive descent // / at the start means the root node so this would match book elements directly below the root node /book author elements that are directly contained within book elements book/authorchild / Meaning of exampleExamplesMeaning

20 XML - XSLT © 2015 University of Greenwich 20 Quick Quiz From Quiz a) From Quiz b) what will appear in a browser when these style sheets are applied to goodBooks.xml?

21 XML - XSLT © 2015 University of Greenwich 21 Quick Quiz From Quiz c) From Quiz d) …and with these style sheets?

22 XML - XSLT © 2015 University of Greenwich 22 Quick Quiz From Quiz e) - rated as : - … and finally

23 XML - XSLT © 2015 University of Greenwich 23 eg3.xsl

24 XML - XSLT From eg3.xsl - books in title order title author eg3.xsl for each book sort by title output title then author match anything output as a table column start an HTML table end of the table

25 XML - XSLT © 2015 University of Greenwich 25 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:sort allows you to sort elements Can also specify descending order pattern to match

26 XML - XSLT © 2015 University of Greenwich 26 Quick Quiz What order would you expect the books to appear in if the code were changed to:

27 XML - XSLT From eg4.xsl List in title order / List in course order course title Full book details rating: : eg4.xsl This is much the same as the previous example output the name of the element output the rating attribute

28 XML - XSLT Spot the difference? eg4.xsl

29 XML - XSLT © 2015 University of Greenwich 29 eg4.xsl Elements in the XML document are repeated with different formatting in the output document Reading element attribute values using xsl:value-of Using local_name() to return the element name from xsl:value-of 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.

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

31 XML - XSLT © 2015 University of Greenwich 31 From eg5.xsl List in title order / eg5.xsl bullet point list

32 XML - XSLT List in course order course title stars WAT! assign the value of the course element to a variable test the value of the variable eg5.xsl

33 XML - XSLT Full book details * ** *** # convert the value of the book rating attribute to stars eg5.xsl form an anchor tag linking to a named anchor in the output document call a named template

34 XML - XSLT rating : : create a named anchor with the name of the book eg5.xsl output the title in bold don’t output the title again unabbreviated XPath relative location path

35 XML - XSLT © 2015 University of Greenwich 35 eg5.xsl The links (anchors) take the reader to internal named targets e.g. could just as easily reference external URLs these could be provided in the XML Two different conditional constructs are used <xsl:if <xsl:if <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! 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 Combination of XSLT and CSS technologies

36 XML - XSLT © 2015 University of Greenwich 36 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.NET PHP PHP4 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 PHP5 DOM supports XSLT

37 XML - XSLT XSLT processing with PHP Requires that PHP is configured to support XSLT

38 XML - XSLT © 2015 University of Greenwich 38 <?php error_reporting(E_ALL); # LOAD XML FILE $doc = new DOMDocument(); $doc->load( 'goodBooks.xml', LIBXML_NOBLANKS ); # START XSLT $xslt = new XSLTProcessor(); $xsl = new DOMDocument(); $xsl->load( 'eg6.xsl', LIBXML_NOCDATA); $xslt->importStylesheet( $xsl ); #PRINT echo $xslt->transformToXML( $doc ); ?> eg6.php create an XSLT processor load the XSLT into a DOM object and import into the XSLT processor transform and output the resulting XHTML load XML file into DOM object

39 XML - XSLT © 2015 University of Greenwich 39 eg6.php

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

41 XML - XSLT © 2015 University of Greenwich 41 eg7.xsl

42 XML - XSLT Server side transform from eg7.xsl - XML data entered into a form eg7.xsl

43 XML - XSLT © 2015 University of Greenwich 43 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: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

44 XML - XSLT © 2015 University of Greenwich 44 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

45 XML - XSLT © 2015 University of Greenwich Questions?


Download ppt "XML - XSLT © 2015 University of Greenwich 1 XML Processing with XSLT e Xtensible Stylesheet Language Transforms Dr Kevin McManus"

Similar presentations


Ads by Google