XSLT Introduction.

1 XSLT Introduction ...
Author: Trevor Mosley
0 downloads 5 Views

1 XSLT Introduction

2 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).

3

4 <. xml version="1. 0". >        

My CD Collection

                                                                                   
TitleArtist
    XSLT Example

5 XSLT Elements ReferenceDescription 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 and to express multiple conditional tests comment Creates a comment node in the result tree

6 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

7 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 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

8 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 element with-param Defines the value of a parameter to be passed into a template

9 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

10 Create an XSL Style Sheet      

My CD Collection

                                                   
TitleArtist
    Create an XSL Style Sheet "cdcatalog.xsl"

11 <. xml version="1. 0" encoding="UTF-8"       Empire Burlesque     Bob Dylan     USA     Columbia     10.90     1985   . . Link the XSL Style Sheet to the XML Document "cdcatalog.xml" "cdcatalog.xsl"

12 RESULT

13 XSLT ElementAn 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 Element The 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).

14 Create an XSL Style Sheet      

My CD Collection

                                           
TitleArtist
..
    Create an XSL Style Sheet "cdcatalog.xsl"

15 <. xml version="1. 0" encoding="UTF-8" Empire Burlesque Bob Dylan USA Columbia 10.90 1985 Hide your heart Bonnie Tyler UK CBS Records 9.90 1988 Greatest Hits Dolly Parton USA RCA 9.90 1982 Still got the blues Gary Moore UK Virgin records 10.20 1990 Eros Eros Ramazzotti EU BMG 1997

16 One night only Bee Gees UK Polydor 10.90 1998 Sylvias Mother Dr.Hook CBS 8.10 1973 Maggie May Rod Stewart UK Pickwick 8.50 1990 Romanza Andrea Bocelli EU Polydor 10.80 1996 When a man loves a woman Percy Sledge USA Atlantic 8.70 1987

17 Black angel Savage Rose EU Mega 10.90 1995 1999 Grammy Nominees Many USA Grammy 10.20 1999 For the good times Kenny Rogers UK Mucik Master 8.70 Big Willie style Will Smith USA Columbia 9.90 1997 Tupelo Honey Van Morrison UK Polydor 8.20 1971

18 Tupelo Honey Soulsville Jorn Hoel Norway WEA 7.90 1996 The very best of Cat Stevens UK Island 8.90 1990 Big Willie style Will Smith USA Columbia 9.90 1997 Tupelo Honey Van Morrison UK Polydor 8.20 1971

19 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

My CD Collection

Title Artist
.
RESULT

20 XSLT Example ExplainedSince an XSL style sheet is an XML document, it always begins with the XML declaration: . The next element, , defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes). The element defines a template. The match="/" attribute associates the template with the root of the XML source document. The content inside the 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 element to select values from the XML elements.

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

22 <. xml version="1. 0" encoding="UTF-8"      

My CD Collection

                                           
TitleArtist
   

23 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

My CD Collection

Title Artist
RESULT

24 XSLT ElementExample 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 element to loop through the XML elements, and display

25 XSLT ElementThe element allows you to do looping in XSLT. The Element The XSL 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.

26 <. xml version="1. 0" encoding="UTF-8"      

My CD Collection

                                                   
TitleArtist
   

27 RESULT

28 XSLT ElementFiltering the Output We can also filter the output from the XML file by adding a criterion to the select attribute in the element. Legal filter operators are: =  (equal) != (not equal) < less than > greater than Take a look at the adjusted XSL style sheet:

29 XSLT Element                        

30 XSLT ElementThe element is used to sort the output. Where to put the Sort Information To sort the output, simply add an element inside the element in the XSL file:

31 <. xml version="1. 0" encoding="UTF-8"      

My CD Collection

                                                                 
TitleArtist
   

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

33 XSLT ElementThe element is used to put a conditional test against the content of the XML file. The Element To put a conditional if test against the content of the XML file, add an element to the XSL document. Syntax   ...some output if the expression is true...

34 <. xml version="1. 0" encoding="UTF-8"      

My CD Collection

                                                                                               
TitleArtistPrice
    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.

35 XSLT ElementThe element is used in conjunction with and to express multiple conditional tests. The Element Syntax       ... some output ...         ... some output ....  

36 <. xml version="1. 0" encoding="UTF-8"      

My CD Collection

                                                                                                             
TitleArtist
         
   

37 RESULT

38 XSLT ElementThe element applies a template to the current element or to the current element's child nodes. The Element If we add a select attribute to the 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:

39 <. xml version="1. 0" encoding="UTF-8"      

My CD Collection

       

     

  Title:    
  Artist:    

40 RESULT

41 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.

42 XML File       Empire Burlesque     Bob Dylan     USA     Columbia     10.90     1985   . .

43 XSL File  

My CD Collection

                                                   
TitleArtist

44 Transforming XML to XHTML in the Browser

46 RESULT

47 Transforming XML to XHTML in the BrowserThe 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

48 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.

49 XML File       Empire Burlesque     Bob Dylan     USA     Columbia     10.90     1985   . .

50 XSL FILE  

My CD Collection

                                                   
TitleArtist

51 PHP Code: Transform XML to XHTML on the Serverload('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); ?>

52 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)) %>